package com.cnhis.cloudhealth.clinical.hosnurse.hossendadvice.bo;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cnhis.cloudhealth.clinical.clidoctor.clisendadvice.dao.CliSendAdviceDao;
import com.cnhis.cloudhealth.commons.commonSql.bo.YaopinKucunBo;
import org.apache.log4j.Logger;
import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosdoctor.doctorparameter.bo.DoctorparameterBo;
import com.cnhis.cloudhealth.clinical.hosdoctor.doctorparameter.dao.DoctorparameterDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hosdoctoradvice.bo.HosDoctorAdviceBo;
import com.cnhis.cloudhealth.clinical.hosnurse.hosexpense.bo.HosExpenseBo;
import com.cnhis.cloudhealth.clinical.hosnurse.hosparams.dao.HosParamsDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hossendadvice.dao.HosSendAdviceDao;
import com.cnhis.cloudhealth.clinical.hosnurse.hosstopadvice.bo.HosStopAdviceBo;
import com.cnhis.cloudhealth.clinical.hosnurse.hosstopadvice.dao.HosStopAdviceDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.StringUtils;

import net.sf.json.JSONObject;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Service
public class HosSendAdviceBo {

	@Resource
	private HosSendAdviceDao hosSendAdviceDao;
	@Resource
	private HosParamsDao hosParamsDao;
	@Resource
	private GetNewId getNewId;
	@Resource
	private HosDoctorAdviceBo hosDoctorAdviceBo;
	@Resource
	private HosStopAdviceBo hosStopAdviceBo;
	@Resource
	private DoctorparameterBo doctorparameterBo;
	@Resource
	private CliSendAdviceDao cliSendAdviceDao;
	@Resource
	private YaopinKucunBo yaopinKucunBo;
	private Logger log=Logger.getLogger(HosSendAdviceBo.class);
	private ModelVo packModelVoPage(String nid,Map map,int pageno,int pagesize) {
		ModelVo mo = new ModelVo();
		try {
			Page p = hosSendAdviceDao.queryForPage(nid, map, pageno, pagesize);
			if (p.getList() != null && p.getList().size() > 0) {
				mo.setList(p.getList());
				mo.setResult(StaticKeys.OH_SUCCESS);
				mo.setTotal(p.getTotalNumber());
				mo.setRows(p.getList());
				mo.setPage(pageno);
				mo.setRecords(p.getTotalNumber());
				mo.setTotalPage(p.getTotalPage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mo;
	}

	public ModelVo execNurseVDC1Edit(Map map) throws Exception {
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		String xml = map.get("lxml").toString();
		JSONObject jsonObject = JSONObject.fromObject(xml);
		List<Map<String, Object>> kbmVDCList = JsonUtil.getList(jsonObject.getJSONArray("VDC1").toString());
		int length = kbmVDCList.size();
		for (int i = 0; i < length; i++) {
			Map<String, Object> m = kbmVDCList.get(i);
			if (!"1".equals(m.get("fcheck").toString())) {
				kbmVDCList.remove(m);
				i--;
				length--;
			}
		}
		map.put("kbmVDCList", kbmVDCList);
		if (isStartTime(map)) {
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("需要指定执行开始时间");
			return vo;
		}
		if (isNumeric(map)) {
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("执行数量不对，请检查");
			return vo;
		}
		String aDate = DateUtils.getDateString(new Date());
		map.put("aDate", aDate);
		// 保存医嘱项目、费用明细、修改收费单据金额
		saveVDC1(map);
		return vo;
	}

	private boolean isStartTime(Map map) throws Exception {
		return hosSendAdviceDao.isStartTime(map) > 0;
	}

	private boolean isNumeric(Map map) throws Exception {
		return hosSendAdviceDao.isNumeric(map) > 0;
	}

	private void saveVDC1(Map map) throws Exception {
		map.put("newID", getNewId.nextId());
		hosSendAdviceDao.saveVDC1(map);
	}

	private Long getVAA07(Map map) throws Exception {
		return hosSendAdviceDao.getVAA07(map);
	}

	private BigDecimal getVB05(Map map) throws Exception {
		return hosSendAdviceDao.getVB05(map);
	}

	private BigDecimal getVB06(Map map) throws Exception {
		return hosSendAdviceDao.getVB06(map);
	}

	private void updatePersonBanlance(Map map) throws Exception {
		hosSendAdviceDao.updatePersonBanlance(map);
	}

	private void updateInpatient(Map map) throws Exception {
		hosSendAdviceDao.updateInpatient(map);
	}

	private void executeAdvice(Map map) throws Exception {
		// 余额表vbm1
		if (hosSendAdviceDao.patientBlanceCount(map) > 0) {
			hosSendAdviceDao.updateBlance(map);
		} else {
			map.put("newID", getNewId.nextId());
			hosSendAdviceDao.insertBlance(map);
		}
		// 余额表VBM2
		if (hosSendAdviceDao.blanceCount(map) > 0) {
			hosSendAdviceDao.updatePatientBlance(map);
		} else {
			map.put("newID", getNewId.nextId());
			hosSendAdviceDao.insertPatientBlance(map);
		}
	}

	public ModelVo execNurseVBM1Update(Map map) throws Exception {
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		int lType = Integer.parseInt(map.get("lType").toString());
		if ("0".equals(map.get("VAA07").toString()) && "2".equals(map.get("lVBM03").toString())) {
			Long VAA07 = getVAA07(map);
			map.put("VAA07", VAA07);
		}
		BigDecimal VBM05, VBM06;// 本次余额 多次累计余额
		if (0 == lType) {// 修正个人余额
			VBM05 = getVB05(map);
			VBM06 = getVB06(map);
			map.put("VBM05", VBM05);
			map.put("VBM06", VBM06);
			// 修改个人余额
			updatePersonBanlance(map);
		}
		if (1 == lType) {// 当前病区
			// 修改当前在院病人余额
			updateInpatient(map);
		}
		if (2 == lType) {// 执行医嘱调用
			executeAdvice(map);
		}
		return vo;
	}

	public ModelVo execNurseVCM1Update(Map map) throws Exception {
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		String json = map.get("lxml").toString();
		List list = JsonUtil.getList(json);
		map.put("list", list);
		hosSendAdviceDao.nurseVCM1Update(map);
		return vo;
	}

	public ModelVo queryPathWay() throws Exception {
		ModelVo mo = new ModelVo();
		Map<String,Object> map = new HashMap<String,Object>();
		List list = null;
		try {
			map.put("aDate", DateUtils.getDateStr(new Date()) + " 23:59:59");
			list = hosSendAdviceDao.queryPathWay(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo selectPatientInfo(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosSendAdviceDao.selectPatientInfo(map);

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo checkMedical(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosSendAdviceDao.checkMedical(map);

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

//	public ModelVo execNurseAdviceSendDrug(Map map) throws Exception {
//		String lxml = map.get("lxml").toString();
//		String BCKXml = map.get("lBCKxml").toString();
//		JSONObject jsonBBX1 = JSONObject.fromObject(lxml);
//		JSONObject jsonBCK1 = JSONObject.fromObject(BCKXml);
//		// 病人登记信息
//		List VAE1 = JsonUtil.getList(jsonBBX1.getJSONArray("VAE1").toString());
//		// 药品
//		List BBX1 = JsonUtil.getList(jsonBBX1.getJSONArray("BBX1").toString());
//		// 科室 部门
//		List BCK1 = JsonUtil.getList(jsonBCK1.getJSONArray("BCK1").toString());
//		map.put("VAE1", VAE1);
//		map.put("BBX1", BBX1);
//		map.put("BCK1", BCK1);
//		map.remove("lxml");
//		map.remove("lBCKxml");
//		String lEDate = DateUtils.addInteger(map.get("lEndDate").toString(), Calendar.DATE, 1);
//		map.put("lEDate", lEDate);
//		map.put("lVAF38", map.get("lEndDate").toString());
//
//		ModelVo mo = new ModelVo();
//		List<Map<String, Object>> list = new ArrayList<>();
//		try {
//			List ls1 = hosSendAdviceDao.nurseAdviceSendDrug1(map);
//			List ls2 = hosSendAdviceDao.nurseAdviceSendDrug2(map);
//			List ls3 = hosSendAdviceDao.nurseAdviceSendDrug3(map);
//			List ls4 = hosSendAdviceDao.nurseAdviceSendDrug4(map);
//			list.addAll(ls1);
//			list.addAll(ls2);
//			list.addAll(ls3);
//			list.addAll(ls4);
//
//		} catch (Exception e) {
//			e.printStackTrace();
//			mo.setResult(StaticKeys.OH_FALSE);
//			return mo;
//		}
//		mo.setList(list);
//		mo.setResult(StaticKeys.OH_SUCCESS);
//		mo.setRows(list);
//		return mo;
//
//	}

	public ModelVo execNurseAdviceSendDrug(Map<String,Object> map,int pageno,int pagesize) throws Exception {
		ModelVo mo = new ModelVo();
		try {
			String ljson = MapUtils.getString(map,"ljson");
			if(StringUtils.isNotBlank(ljson)) {
				JSONObject job = JSONObject.fromObject(ljson);
				if (job != null && !job.isEmpty()) {
					List<Map<String, Object>> bbxList = JsonUtil.parserToList(job.getJSONArray("bbx1"));
					map.put("bbxList", bbxList);
				}
			}
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String lEndDatestr = MapUtils.getString(map,"lEndDate","");
			if(org.apache.commons.lang3.StringUtils.isBlank(lEndDatestr)){
				Date ndate = new Date();
				DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
				String s = df2.format(ndate);
				lEndDatestr = s;
			}
			Date lVAF38 = df.parse(lEndDatestr);
			Date lEndDate = lVAF38;
			Date lEDate = lEndDate;
//			Date lEDate = org.apache.commons.lang3.time.DateUtils.addDays(lVAF38,1);
			map.put("lVAF38",lVAF38);
			map.put("lEDate",lEDate);
			map.put("lEndDate",lEndDate);
//			Page p = hosSendAdviceDao.queryForPage("HosSendAdvice.advice_send_drug", map, pageno, pagesize);
			Integer adint = hosSendAdviceDao.advicesendint(map);
			Page p = hosSendAdviceDao.advicesendList(map, pageno, pagesize);
			if (p.getList() != null && p.getList().size() > 0) {
				List<Map<String, Object>> vaf01List = new ArrayList<Map<String, Object>>();
				for(Object po:p.getList()){
					if(po instanceof Map){
						Map<String,Object> poo = (Map<String,Object>)po;
						long vaf01 = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01",0l);
						long vaf01a = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01a",0l);
						String bda01 = org.apache.commons.collections.MapUtils.getString(poo,"bda01","");
						Integer vaf11 = org.apache.commons.collections.MapUtils.getInteger(poo,"vaf11",0);
						poo.put("vafid", vaf01);
						poo.put("vafida", vaf01a);
						poo.put("lBDA01", bda01);
						poo.put("vaf11", vaf11);
						vaf01List.add(poo);
					}
					
				}
				map.put("poo",vaf01List);
				List<Map<String,Object>> list = hosSendAdviceDao.queryForList("HosSendAdvice.advice_send_price", map);
				if(list!=null&&list.size()>0){
					for(Object po:p.getList()){
						Map<String,Object> poo = (Map<String,Object>)po;
						List<Map<String,Object>> listp = new ArrayList<Map<String,Object>>();
						
						long vaf01 = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01");
						for(int i=0;i<list.size();i++){
							Map<String,Object> pooo = list.get(i);
							long vaf01i = org.apache.commons.collections.MapUtils.getLong(pooo,"vafid");
							if(vaf01 == vaf01i){
								Double lvafidsum = org.apache.commons.collections.MapUtils.getDouble(pooo,"lvafidsum",0.0);
								poo.put("lmoney",lvafidsum);
								listp.add(pooo);
								poo.put("priceList",listp);
								double price = 0.0;
								for(Map<String,Object> mm:listp){
									Double vbd05 = org.apache.commons.collections.MapUtils.getDouble(mm,"vbd05",0.0);
									price+=vbd05;
								}
								poo.put("price",price);
							}	
						}
					}
				}
				mo.setList(p.getList());
				mo.setResult(StaticKeys.OH_SUCCESS);
				mo.setTotal(p.getTotalNumber());
				mo.setRows(p.getList());
				mo.setUserdata("{totalsize:"+p.getTotalNumber()+"}");
				mo.setPage(pageno);
				mo.setRecords(adint);
				mo.setTotalPage(p.getTotalPage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mo;
	}
	
	//Nurse_Advice_Send_Other
	public ModelVo otherAdviceList(Map<String,Object> map,int pageno,int pagesize) throws Exception {
		ModelVo mo = new ModelVo();
		try {
			String ljson = MapUtils.getString(map,"ljson");
			if(StringUtils.isNotBlank(ljson)) {
				JSONObject job = JSONObject.fromObject(ljson);
				if (job != null && !job.isEmpty()) {
					List<Map<String, Object>> bbxList = JsonUtil.parserToList(job.getJSONArray("bbx1"));
					map.put("bbxList", bbxList);
				}
			}
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String lEndDatestr = MapUtils.getString(map,"lEndDate","");
			if(org.apache.commons.lang3.StringUtils.isBlank(lEndDatestr)){
				Date ndate = new Date();
				DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
				String s = df2.format(ndate);
				lEndDatestr = s;
			}
			Date lVAF38 = df.parse(lEndDatestr);
			Date lEndDate = lVAF38;
			Date lEDate = lEndDate;
//			Date lEDate = org.apache.commons.lang3.time.DateUtils.addDays(lVAF38,1);
			map.put("lVAF38",lVAF38);
			map.put("lEDate",lEDate);
			map.put("lEndDate",lEndDate);
//			Page p = hosSendAdviceDao.queryForPage("HosSendAdvice.other_advice_send_drug", map, pageno, pagesize);
			Integer adint = hosSendAdviceDao.otheradvicesendint(map);
			Page p = hosSendAdviceDao.otheradvicesendList(map, pageno, pagesize);
			if (p.getList() != null && p.getList().size() > 0) {
				List<Map<String, Object>> vaf01List = new ArrayList<Map<String, Object>>();
				for(Object po:p.getList()){
					if(po instanceof Map){
						Map<String,Object> poo = (Map<String,Object>)po;
						long vaf01 = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01",0l);
						long vaf01a = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01a",0l);
						String bda01 = org.apache.commons.collections.MapUtils.getString(poo,"bda01","");
						Integer vaf11 = org.apache.commons.collections.MapUtils.getInteger(poo,"vaf11",0);
						poo.put("vafid", vaf01);
						poo.put("vafida", vaf01a);
						poo.put("lBDA01", bda01);
						poo.put("vaf11", vaf11);
						vaf01List.add(poo);
					}
				}
				map.put("poo",vaf01List);
				List<Map<String,Object>> list = hosSendAdviceDao.queryForList("HosSendAdvice.other_advice_send_price", map);
				if(list!=null&&list.size()>0){
					for(Object po:p.getList()){
						Map<String,Object> poo = (Map<String,Object>)po;
						List<Map<String,Object>> listp = new ArrayList<Map<String,Object>>();
						
						long vaf01 = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01");
						for(int i=0;i<list.size();i++){
							Map<String,Object> pooo = list.get(i);
							long vaf01i = org.apache.commons.collections.MapUtils.getLong(pooo,"vafid");
							if(vaf01 == vaf01i){
								Double lvafidsum = org.apache.commons.collections.MapUtils.getDouble(pooo,"lvafidsum",0.0);
								poo.put("lmoney",lvafidsum);
								listp.add(pooo);
								poo.put("priceList",listp);
								double price = 0.0;
								for(Map<String,Object> mm:listp){
									Double vbd05 = org.apache.commons.collections.MapUtils.getDouble(mm,"vbd05",0.0);
									price+=vbd05;
								}
								poo.put("price",price);
							}	
						}
					}
				}
				mo.setList(p.getList());
				mo.setResult(StaticKeys.OH_SUCCESS);
				mo.setTotal(p.getTotalNumber());
				mo.setUserdata("{totalsize:"+p.getTotalNumber()+"}");
				mo.setRows(p.getList());
				mo.setPage(pageno);
				mo.setRecords(adint);
				mo.setTotalPage(p.getTotalPage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mo;
	}
	
	public ModelVo execNurseAdviceSendDrugall(Map<String,Object> map,int pageno,int pagesize) throws Exception {
		ModelVo mo = new ModelVo();
		try {
			String ljson = MapUtils.getString(map,"ljson");
			if(StringUtils.isNotBlank(ljson)) {
				JSONObject job = JSONObject.fromObject(ljson);
				if (job != null && !job.isEmpty()) {
					List<Map<String, Object>> bbxList = JsonUtil.parserToList(job.getJSONArray("bbx1"));
					map.put("bbxList", bbxList);
				}
			}
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String lEndDatestr = MapUtils.getString(map,"lEndDate","");
			if(org.apache.commons.lang3.StringUtils.isBlank(lEndDatestr)){
				Date ndate = new Date();
				DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
				String s = df2.format(ndate);
				lEndDatestr = s;
			}
			Date lVAF38 = df.parse(lEndDatestr);
			Date lEndDate = lVAF38;
			Date lEDate = lEndDate;
//			Date lEDate = org.apache.commons.lang3.time.DateUtils.addDays(lVAF38,1);
			map.put("lVAF38",lVAF38);
			map.put("lEDate",lEDate);
			map.put("lEndDate",lEndDate);
			int yaopin = 0;
			Map<String, Object>  mapx = new HashMap<>();
			mapx.putAll(map);
			Page p = hosSendAdviceDao.advicesendallList(mapx, 1, pagesize);
			if (p.getList() != null && p.getList().size() > 0) {
				List<Map<String, Object>> vaf01Listlong = new ArrayList<Map<String, Object>>();
				List<Map<String, Object>> vaf01Listtemp = new ArrayList<Map<String, Object>>();
				List<Map<String, Object>> vaf01List = new ArrayList<Map<String, Object>>();
				for(Object po:p.getList()){
					if(po instanceof Map){
						Map<String,Object> poo = (Map<String,Object>)po;
						long vaf01 = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01",0l);
						long vaf01a = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01a",0l);
						String bda01 = org.apache.commons.collections.MapUtils.getString(poo,"bda01","");
						Integer vaf11 = org.apache.commons.collections.MapUtils.getInteger(poo,"vaf11",0);
						poo.put("vafid", vaf01);
						poo.put("vafida", vaf01a);
						poo.put("lBDA01", bda01);
						if(bda01.equals("1")||bda01.equals("2")||bda01.equals("3")){
							yaopin = 1;
						}
						poo.put("vaf11", vaf11);
						vaf01List.add(poo);
						if(vaf11==1){
							vaf01Listlong.add(poo);
						}else{
							vaf01Listtemp.add(poo);
						}
						
					}
					
				}
				
				if(vaf01Listlong!=null && vaf01Listlong.size()>0){
					Map<String, Object>  map1 = new HashMap<>();
					map1.putAll(map);
					map1.put("vaf",vaf01Listlong);
					map1.put("lSign", 6);
					map1.put("lOneDay", 0);
					map1.put("lFlag", 1);
					mo = execNurseLongAdviceSendall(map1);
					if(mo.getResult().equals(StaticKeys.OH_FALSE)){
						return mo;
					}
				}
				if(vaf01Listtemp!=null && vaf01Listtemp.size()>0){
					Map<String, Object> map11 = new HashMap<>();
					map11.putAll(map);
					map11.put("vaf",vaf01Listtemp);
					map11.put("lSign", 6);
					map11.put("lOneDay", 0);
					map11.put("lFlag", 1);
					map11.put("lSend", 0);	
					mo = execNurseTempAdviceSendall(map11);
					if(mo.getResult().equals(StaticKeys.OH_FALSE)){
						return mo;
					}
					String pvalue = doctorparameterBo.getSysValue(100,105001,394);
					if(pvalue.equals('1')){
						Map<String, Object> map22 = new HashMap<>();
						map22.putAll(map);
						map22.put("vaf",vaf01Listtemp);
						mo = nurseLisBillsPrintAutoall(map22);
						if(mo.getResult().equals(StaticKeys.OH_FALSE)){
							return mo;
						}
					}
					
				}
				Map<String, Object> map12 = new HashMap<>();
				map12.putAll(map);
				map12.put("vaf",vaf01List);
				String paraa = doctorparameterBo.getSysValue(100,105001,155);
				String parab = doctorparameterBo.getSysValue(100,105001,156);
				String parac = doctorparameterBo.getSysValue(100,105001,157);
				List<Map<String, Object>> TParaList = new ArrayList<Map<String, Object>>();
				Map<String, Object> TParamap = new HashMap<String, Object>();
				TParamap.put("paraa", paraa);
				TParamap.put("parab", parab);
				TParamap.put("parac", parac);
				TParaList.add(TParamap);
				map12.put("TPara", TParaList);
				mo = hosStopAdviceBo.execNurseAdviceAutoStopall(map12);
				if(mo.getResult().equals(StaticKeys.OH_FALSE)){
					return mo;
				}
			}

			if(yaopin==1){
				mo.setResultMsg("有药品医嘱!");
			}
			mo.setResult(StaticKeys.OH_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		return mo;
	}
	//全部查找发送非药品
	public ModelVo otherAdviceListall(Map<String,Object> map,int pageno,int pagesize) throws Exception {
		ModelVo mo = new ModelVo();
		try {
			String ljson = MapUtils.getString(map,"ljson");
			if(StringUtils.isNotBlank(ljson)) {
				JSONObject job = JSONObject.fromObject(ljson);
				if (job != null && !job.isEmpty()) {
					List<Map<String, Object>> bbxList = JsonUtil.parserToList(job.getJSONArray("bbx1"));
					map.put("bbxList", bbxList);
				}
			}
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String lEndDatestr = MapUtils.getString(map,"lEndDate","");
			if(org.apache.commons.lang3.StringUtils.isBlank(lEndDatestr)){
				Date ndate = new Date();
				DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
				String s = df2.format(ndate);
				lEndDatestr = s;
			}
			Date lVAF38 = df.parse(lEndDatestr);
			Date lEndDate = lVAF38;
			Date lEDate = lEndDate;
			map.put("lVAF38",lVAF38);
			map.put("lEDate",lEDate);
			map.put("lEndDate",lEndDate);
			int shoushu = 0;
			Map<String, Object>  mapx = new HashMap<>();
			mapx.putAll(map);
			Page p = hosSendAdviceDao.otheradvicesendallList(mapx, 1, pagesize);
			
			if (p.getList() != null && p.getList().size() > 0) {
				List<Map<String, Object>> vaf01Listlong = new ArrayList<Map<String, Object>>();
				List<Map<String, Object>> vaf01Listtemp = new ArrayList<Map<String, Object>>();
				List<Map<String, Object>> vaf01List = new ArrayList<Map<String, Object>>();
				for(Object po:p.getList()){
					if(po instanceof Map){
						Map<String,Object> poo = (Map<String,Object>)po;
						long vaf01 = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01",0l);
						long vaf01a = org.apache.commons.collections.MapUtils.getLong(poo,"vaf01a",0l);
						String bda01 = org.apache.commons.collections.MapUtils.getString(poo,"bda01","");
						Integer vaf11 = org.apache.commons.collections.MapUtils.getInteger(poo,"vaf11",0);
						poo.put("vafid", vaf01);
						poo.put("vafida", vaf01a);
						poo.put("lBDA01", bda01);
						if(bda01.equals("S")){
							shoushu = 1;
						}
						poo.put("vaf11", vaf11);
						vaf01List.add(poo);
						if(vaf11==1){
							vaf01Listlong.add(poo);								
						}else{
							vaf01Listtemp.add(poo);
						}
					}
				}
					
				
				if(vaf01Listlong!=null && vaf01Listlong.size()>0){
					Map<String, Object>  map1 = new HashMap<>();
					map1.putAll(map);
					map1.put("vaf",vaf01Listlong);
					map1.put("lSign", 6);
					map1.put("lOneDay", 0);
					map1.put("lFlag", 1);
					mo = execNurseLongAdviceSendall(map1);	
					if(mo.getResult().equals(StaticKeys.OH_FALSE)){
						return mo;
					}
				}
				if(vaf01Listtemp!=null && vaf01Listtemp.size()>0){
					Map<String, Object> map11 = new HashMap<>();
					map11.putAll(map);
					map11.put("lSign", 6);
					map11.put("lOneDay", 0);
					map11.put("lFlag", 1);
					map11.put("lSend", 0);					
					map11.put("vaf",vaf01Listtemp);
					mo = execNurseTempAdviceSendall(map11);		
					if(mo.getResult().equals(StaticKeys.OH_FALSE)){
						return mo;
					}
					String pvalue = doctorparameterBo.getSysValue(100,105001,394);
					if(pvalue.equals('1')){
						Map<String, Object>  map22 = new HashMap<>();
						map22.putAll(map);
						map22.put("vaf",vaf01Listtemp);
						mo = nurseLisBillsPrintAutoall(map22);
						if(mo.getResult().equals(StaticKeys.OH_FALSE)){
							return mo;
						}
					}
				}
				Map<String, Object> map12 = new HashMap<>();
				map12.putAll(map);
				map12.put("vaf",vaf01List);
				String paraa = doctorparameterBo.getSysValue(100,105001,155);
				String parab = doctorparameterBo.getSysValue(100,105001,156);
				String parac = doctorparameterBo.getSysValue(100,105001,157);
				List<Map<String, Object>> TParaList = new ArrayList<Map<String, Object>>();
				Map<String, Object> TParamap = new HashMap<String, Object>();
				TParamap.put("paraa", paraa);
				TParamap.put("parab", parab);
				TParamap.put("parac", parac);
				TParaList.add(TParamap);
				map12.put("TPara", TParaList);
				mo = hosStopAdviceBo.execNurseAdviceAutoStopall(map12);
				if(mo.getResult().equals(StaticKeys.OH_FALSE)){
					return mo;
				}
			}

				
			mo.setResult(StaticKeys.OH_SUCCESS);
			if(shoushu==1){
				mo.setResultMsg("有手术医嘱!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		return mo;
	}
	
	public ModelVo drugAdviceList(Map map,int currPage,int pageSize) throws Exception {
		String lxml = map.get("lxml").toString();
		String BCKXml = map.get("lBCKxml").toString();
		JSONObject jsonBBX1 = JSONObject.fromObject(lxml);
		JSONObject jsonBCK1 = JSONObject.fromObject(BCKXml);
		// 病人登记信息
//		List VAE1 = JsonUtil.getList(jsonBBX1.getJSONArray("VAE1").toString());
		// 药品
		List BBX1 = JsonUtil.getList(jsonBBX1.getJSONArray("BBX1").toString());
		// 科室 部门
		List BCK1 = JsonUtil.getList(jsonBCK1.getJSONArray("BCK1").toString());
//		map.put("VAE1", VAE1);
		map.put("BBX1", BBX1);
		map.put("BCK1", BCK1);
		map.remove("lxml");
		map.remove("lBCKxml");
		String lEDate = DateUtils.addInteger(map.get("lEndDate").toString(), Calendar.DATE, 1);
		map.put("lEDate", lEDate);
		map.put("lVAF38", map.get("lEndDate").toString());
        List BDA12 = hosSendAdviceDao.getBDA12(map);
        map.put("BDA12", BDA12);
		ModelVo mo = new ModelVo();
		Page pg = new Page();
		try {
			pg=hosSendAdviceDao.drugAdviceList(map, currPage, pageSize);

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(pg.getList());
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(pg.getList());
		return mo;
		
	}
	private String para209() throws Exception {
		return hosSendAdviceDao.getSysParamValue();
	}

	private List<Map<String, Object>> getTempBAD(Map map) throws Exception {
		return hosSendAdviceDao.getTempBAD(map);
	}

	private List<Map<String, Object>> tmpAdvCur(Map map) throws Exception {
		return hosSendAdviceDao.tmpAdvCur(map);
	}

	private List<Map<String, Object>> getAdvicePrice(Map map) throws Exception {
		return hosSendAdviceDao.getAdvicePrice(map);
	}

	public ModelVo execNurseAdviceSendDrugPrice(Map map) throws Exception {
		String lxml = map.get("lxml").toString();
		String BCKXml = map.get("lBCKxml").toString();
		JSONObject jsonBBX1 = JSONObject.fromObject(lxml);
		JSONObject jsonBCK1 = JSONObject.fromObject(BCKXml);
		// 病人登记信息
		List VAE1 = JsonUtil.getList(jsonBBX1.getJSONArray("VAE1").toString());
		// 药品
		List BBX1 = JsonUtil.getList(jsonBBX1.getJSONArray("BBX1").toString());
		// 科室 部门
		List BCK1 = JsonUtil.getList(jsonBCK1.getJSONArray("BCK1").toString());
		map.put("VAE1", VAE1);
		map.put("BBX1", BBX1);
		map.put("BCK1", BCK1);

		map.remove("lxml");
		map.remove("lBCKxml");

		String lEDate = DateUtils.addInteger(map.get("lEndDate").toString(), Calendar.DATE, 1);
		map.put("lEDate", lEDate);
		map.put("lVAF38", map.get("lEndDate").toString());
		String para209 = para209();
		map.put("para209", para209);
		ModelVo mo = new ModelVo();

		List<Map<String, Object>> list = new ArrayList<>();
		List AdvPrice = new ArrayList<>();// 计价
		List price = null;
		try {
			List BDA12 = getTempBAD(map);
			map.put("BDA12", BDA12);
			List<Map<String, Object>> tmpAdvCur = tmpAdvCur(map);
			if (tmpAdvCur.size() != 0) {

				for (Map<String, Object> mm : tmpAdvCur) {
					map.put("lVAFID", mm.get("vaf01").toString());
					map.put("lVAFIDA", mm.get("vaf01a").toString());
					map.put("lBDA01", mm.get("bda01").toString());
					map.put("VAF04", mm.get("vaf04").toString());
					price = getAdvicePrice(map);
					AdvPrice.addAll(price);
				}
			}
			map.put("AdvPrice", AdvPrice);
			list = hosSendAdviceDao.advicePrice(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	//
	private String transferLongToDate(String dateFormat, String millSec) {// yyyy-MM-dd
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		boolean bool = true;
		Date date = new Date();			
		try {
			date = sdf.parse(millSec);
		} catch (ParseException e) {
			bool = false;
		}
		if(!bool){
			Long mill = Long.parseLong(millSec);																// HH:mm:s			
			date = new Date(mill);
		}
		return sdf.format(date);	
	}

	//
	public boolean isDateStr(String str) {
		boolean bool = true;
		try {			
			new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(str);
		} catch (Exception e) {
			bool = false;
		}
		return bool;
	}

	private List getLongAdvice(Map map, List<Map<String, Object>> VAF2) throws Exception {
		List<Map<String, Object>> longAdvice = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> m : VAF2) {
			if ("1".equals(m.get("vaf11").toString())) {
				if (m.containsKey("vaf62")) {
					if (null != m.get("vaf62")) {
						if (Double.parseDouble(m.get("vaf62").toString()) < 0) {
							m.put("vaf62", 0);
						}
					} else {
						m.put("vaf62", 0);
					}
				} else {
					m.put("vaf62", 0);
				}
				longAdvice.add(m);
			}
		}
		for (Map<String, Object> m : longAdvice) {
			if (m.containsKey("vaf36")) {
				if (null != m.get("vaf36")) {
					String vaf36 = m.get("vaf36").toString();
					if (!isDateStr(vaf36)) {
						String date = transferLongToDate("yyyy-MM-dd HH:mm:ss", vaf36);
						m.put("vaf36", date);
					}
				}
			}
			if (m.containsKey("vaf37")) {
				if (null != m.get("vaf37")) {
					String vaf37 = m.get("vaf37").toString();
					if (!isDateStr(vaf37)) {
						String date = transferLongToDate("yyyy-MM-dd HH:mm:ss", vaf37);
						m.put("vaf37", date);
					}
				}
			}
			if (m.containsKey("vaf38")) {
				if (null != m.get("vaf38")) {
					String vaf38 = m.get("vaf38").toString();
					if (!isDateStr(vaf38)) {
						String date = transferLongToDate("yyyy-MM-dd HH:mm:ss", vaf38);
						m.put("vaf38", date);
					}
				}
			}
			if (m.containsKey("avaf38")) {
				if (null != m.get("avaf38")) {
					String aVAF38 = m.get("avaf38").toString();
					if (!isDateStr(aVAF38)) {
						String date = transferLongToDate("yyyy-MM-dd HH:mm:ss", aVAF38);
						m.put("avaf38", date);

					}
				}
			}

			if (m.containsKey("vaf47")) {
				if (null != m.get("vaf47")) {
					String vaf47 = m.get("vaf47").toString();
					if (!isDateStr(vaf47)) {
						String date = transferLongToDate("yyyy-MM-dd HH:mm:ss", vaf47);
						m.put("vaf47", date);

					}
				}
			}
			if (m.containsKey("vaf50")) {
				if (null != m.get("vaf50")) {
					String vaf50 = m.get("vaf50").toString();
					if (!isDateStr(vaf50)) {
						String date = transferLongToDate("yyyy-MM-dd HH:mm:ss", vaf50);
						m.put("vaf50", date);

					}
				}
			}
		}
		return longAdvice;
	}

	private Map getParamValue() throws Exception {
		return hosSendAdviceDao.getParamValue();
	}

	private List queryTempLongAdvice(Map map) throws Exception {
		return hosSendAdviceDao.queryTempLongAdvice(map);
	}

	private boolean checkDateDiff(Map map, List tmpBDA) throws Exception {
		map.put("tmpBDA", tmpBDA);
		return hosSendAdviceDao.checkDateDiff(map) > 0;
	}

	private boolean checkDisable(Map map, List tmpBDA) throws Exception {
		map.put("tmpBDA", tmpBDA);
		return hosSendAdviceDao.checkDisable(map) > 0;
	}

	private List getVBD(Map map, List BDA, List tempVBD) throws Exception {
		map.put("tmpBDA", BDA);
		map.put("tempVBD", tempVBD);
		int max = hosSendAdviceDao.getMaxValue(map);
		int min = 1;
		List<Map<String, Object>> VBDList = new ArrayList<Map<String, Object>>();
		List<Map<String, Integer>> mmlist = new ArrayList<Map<String, Integer>>();
		List ls = null;
		
		if (max >= 2) {
			while (min <= max) {
				 Map<String, Integer> mmp = new HashMap<String, Integer>();
				 mmp.put("anum", min);
				 mmlist.add(mmp);
				 min++;
			}
			map.put("aMday", mmlist);
			ls = hosSendAdviceDao.getVBD(map);
			VBDList.addAll(ls);
		}
		
		return VBDList;
	}

	private boolean checkPriceContent(Map map) throws Exception {
		return hosSendAdviceDao.checkPriceContent(map) > 0;
	}

	public ModelVo execNurseLongAdviceSendJudge(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		String lxml = map.get("lXml").toString();
		String BCKXml = map.get("lBCKxml").toString();
		JSONObject jsonVAF2 = JSONObject.fromObject(lxml);
		JSONObject jsonBCK1 = JSONObject.fromObject(BCKXml);
		// 医嘱
		List<Map<String, Object>> VAF2 = JsonUtil.getList(jsonVAF2.getJSONArray("VAF1").toString());
		// 药品
		// List<Map<String, Object>> TPara =
		// JsonUtil.getList(jsonBBX1.getJSONArray("TPara").toString());
		// 科室 部门
		List<Map<String, Object>> BCK1 = JsonUtil.getList(jsonBCK1.getJSONArray("BCK1").toString());

		map.remove("lXml");
		map.remove("lBCKxml");
		List list = null;
		// 格式化时间
		List<Map<String, Object>> longAdvice = getLongAdvice(map, VAF2);
		List<Map<String, Object>> retabTmp = new ArrayList<Map<String, Object>>();
		if (longAdvice.size() == 0) {
			HashMap hs = new HashMap();
			hs.put("vaf01", 0);
			hs.put("bby01", 0);
			hs.put("sflag", 0);
			hs.put("beizhu", "");
			retabTmp.add(hs);
		}
		Map paramValue = getParamValue();
		String currDate = DateUtils.getDateString(new Date());
		map.putAll(paramValue);// 系统参数
		map.put("aStr", "0000000000");
		map.put("curDate", currDate);
		map.put("curtime", currDate.substring(10));
		map.put("BBY31", DateUtils.getDateStr(new Date()) + " 23:59:59.999");
		map.put("eVAF38", map.get("lEndDate").toString());
		map.put("vaj47", new SimpleDateFormat("yyyy-MM").format(new Date()) + "-01");
		// 医嘱数据
		map.put("VAF2", longAdvice);
		List TmpVAFzt = queryTempLongAdvice(map);

		map.put("TmpVAFzt", TmpVAFzt);
		List BDA = getTempBAD(map);
		if (retabTmp.size() == 0 && checkDateDiff(map, BDA)) {
			List ls = hosSendAdviceDao.tempData(map);//
			retabTmp.addAll(ls);
		}
		BDA = hosSendAdviceDao.getBDA(map);
		// 医嘱发送的终止时间小于起始时间
		if (retabTmp.size() == 0 && checkDisable(map, BDA)) {
			List ls = hosSendAdviceDao.tempData1(map);//
			retabTmp.addAll(ls);
		}
		List tempVBD = hosSendAdviceDao.tempVBD(map);
		List VBDList = getVBD(map, BDA, tempVBD);
		VBDList.addAll(tempVBD);
		map.put("tmpVBDList", VBDList);
		// 医嘱计价没有计价内容
		if (retabTmp.size() == 0 && checkPriceContent(map)) {
			List ls = hosSendAdviceDao.queryPriceContent(map);
			retabTmp.addAll(ls);
		}
		VBDList = hosSendAdviceDao.updateVBD(map);
		// 修改计价科室
		map.put("BCK1", BCK1);
		if (BCK1.size() != 0 && hosSendAdviceDao.VAFCount(map) > 0) {
			VBDList = hosSendAdviceDao.updatePriceOffice(map);
		}
		// 医嘱计价明细
		map.put("tmpVBDList", VBDList);
		List VAJList = hosSendAdviceDao.queryVaj2(map);

		// 收费单据
		map.put("tmpVAJ", VAJList);
		// List VAIList = hosSendAdviceDao.queryVai2(map);

		// 判断医嘱作废或停止
		if (retabTmp.size() == 0 && getStopAdvice(map).size() > 0) {
			List ls = hosSendAdviceDao.getStopAdvice(map);
			retabTmp.addAll(ls);
		}
		// 判断医嘱作废或发送
		if (retabTmp.size() == 0 && getSendAdvice(map).size() > 0) {
			List ls = hosSendAdviceDao.getSendAdvice(map);
			retabTmp.addAll(ls);
		}
		// 药品禁售
		if (retabTmp.size() == 0 && getDisableSale(map).size() > 0) {
			List ls = hosSendAdviceDao.getDisableSale(map);
			retabTmp.addAll(ls);
		}
		VAJList = hosSendAdviceDao.queryVaj2(map);
		map.put("TmpVAJ", VAJList);
		// 限制销售金额
		if ("1".equals(paramValue.get("gpara69").toString()) || "2".equals(paramValue.get("gpara69").toString())) {
			if (retabTmp.size() == 0 && checkVaj2(VAJList)) {
				List ls = hosSendAdviceDao.limitMoney(map);
				retabTmp.addAll(ls);
			}
		}
		// 药品库存
		if (retabTmp.size() == 0 && ("2".equals(paramValue.get("gpara21").toString())) && checkStore(map)) {
			List ls = hosSendAdviceDao.getLimitStore(map);
			retabTmp.addAll(ls);
		}
		map.put("retabTmp", retabTmp);
		list = hosSendAdviceDao.paramLimit(map);
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	// 判断医嘱作废或停止
	private List getStopAdvice(Map map) throws Exception {
		return hosSendAdviceDao.getStopAdvice(map);
	}

	// 判断医嘱作废或发送
	private List getSendAdvice(Map map) throws Exception {
		return hosSendAdviceDao.getSendAdvice(map);
	}

	// 药品禁售
	private List getDisableSale(Map map) throws Exception {
		return hosSendAdviceDao.getDisableSale(map);
	}

	private boolean checkVaj2(List<Map<String, Object>> VAJList) throws Exception {
		for (Map m : VAJList) {
			if (m.containsKey("bdn01") && m.containsKey("bag46")) {
				if (Integer.parseInt(m.get("bdn01").toString()) > 2
						&& Integer.parseInt(m.get("bag46").toString()) > 2) {
					return true;
				}
			}
		}
		return false;
	}

	// 药品库存
	private boolean checkStore(Map map) throws Exception {
		return hosSendAdviceDao.checkStore(map) > 0;
	}
	// 用于执行或发送医嘱时，判断是否药品 1770

//	private Boolean checkMedicial(List<Map<String, Object>> bby1) throws Exception {
//
//		if (bby1 != null && bby1.size() > 0) {
//			for (Map<String, Object> m : bby1) {
//				long bby01 = MapUtils.getLong(m,"bby01",0l);
//				String bby05 = hosSendAdviceDao.getBBY05(bby01);
//
//
//			}
//
//		}
//
//	}

	/* ScriptId(6430) */
	/* Description(医嘱判断药房库存，按执行科室来判断) */
	public ModelVo checkStoreNumber(Map<String,Object> map) throws Exception {
		ModelVo mo = new ModelVo();
		mo.setResult(StaticKeys.OH_SUCCESS);
		String aParamno = hosSendAdviceDao.getaParamno();
		String bby1 = MapUtils.getString(map,"ljson");
		if(StringUtils.isBlank(bby1)){
			return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，项目信息为空！");
		}
		JSONObject job = JSONObject.fromObject(bby1);
		if(job==null||job.isEmpty()){
			return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，项目相关信息格式不正确，请检查！");
		}
		List<Map<String,Object>> bbyList = JsonUtil.parserToList(job.getJSONArray("bby1"));
		map.put("aParamno", aParamno);
		map.put("lACF01", 2);
		List<Map<String,Object>> rList = new ArrayList<Map<String,Object>>();
		for (Map<String, Object> mm : bbyList) {
			Map<String,Object> rMap = new HashMap<String,Object>();
			long bby01 = MapUtils.getLongValue(mm,"bby01",0l);
			long bck01d = MapUtils.getLongValue(mm,"bck01d",0l);
			map.put("lBCK01", bck01d);
			map.put("lBBY01", bby01);
			String bby05 = hosSendAdviceDao.getBBY05(mm);
			if(StringUtils.isNotBlank(bby05)) {
				int amout = hosSendAdviceDao.getAmount(map);
				if (amout <= 0) {
					mo.setResult(StaticKeys.OH_FALSE);
					rMap.put("bby01",bby01);
					rMap.put("bby05",bby05);
					rMap.put("msg", bby05+"库存不足！");
					rList.add(rMap);
				}
			}else{
				mo.setResult(StaticKeys.OH_FALSE);
				rMap.put("bby01", bby01);
				rMap.put("msg", "不是药品！");
				rList.add(rMap);
			}
		}
		mo.setList(rList);
		mo.setTotal(rList.size());
		mo.setRows(rList);
		return mo;
	}
	public List checkStoreNumber1(List<Map<String, Object>> vAF2) throws Exception {
		List<String> rList = new ArrayList<String>();		
		for (Map<String, Object> mm : vAF2) {
			mm.put("lACF01", 2);
			long bby01 = MapUtils.getLongValue(mm,"bby01",0l);
			long bck01b = MapUtils.getLongValue(mm,"bck01b",0l);
			mm.put("lBCK01", bck01b);
			mm.put("lBBY01", bby01);
			String bby05 = hosSendAdviceDao.getBBY05(mm);
			if(StringUtils.isNotBlank(bby05)) {
				int amout = hosSendAdviceDao.getAmount(mm);
				if (amout <= 0) {
					rList.add(bby05+" ");
				}
			}
		}
		return rList;
	}
	//长嘱发送
	public ModelVo execNurseLongAdviceSend(Map map) throws Exception {
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		long start1 = System.currentTimeMillis();
		map.put("tablename", String.valueOf(start1));
		log.info("============================开始时间===========================:"+(start1));
		try
		{
		    JSONObject jsonVAF2 = JSONObject.fromObject(map.get("lXml").toString());
	        JSONObject jsonBCK1 = JSONObject.fromObject(map.get("lBCKxml").toString());
	        // 医嘱
	        List<Map<String, Object>> VAF2 = JsonUtil.getList(jsonVAF2.getJSONArray("VAF1").toString());
	        // 科室 部门
	        List<Map<String, Object>> BCK1 = JsonUtil.getList(jsonBCK1.getJSONArray("BCK1").toString());
	        map.put("BCK1", BCK1);
	        map.remove("lXml");
	        map.remove("lBCKxml");
	        // 药品库存量
//	      List amout = checkStoreNumber1(VAF2);
//	      if (amout != null && !amout.isEmpty()) {
//	          mo.setResult(StaticKeys.OH_FALSE);
//	          mo.setResultMsg(amout.toString() + "库存不足");
//	          return mo;
//	      }
	        // 只检索长嘱
	        List<Map<String, Object>> longAdvice = getLongAdvice(map, VAF2);
	        if (longAdvice.size() == 0) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            return mo;
	        }
	       
	        Map params = getParameters();
	        map.putAll(params);
	        
	        
	        // 长嘱信息	       
			map.put("TmpVAFzt", longAdvice);
			longAdvice = hosSendAdviceDao.getTempVAF2(map);
			long start2 = System.currentTimeMillis();
			log.info("============================getTempVAF2时间===========================:"+(start2-start1));
	        // 中、成药、西药
	        if (checkBAD04(map) && "1".equals(params.get("para140").toString())) {
	            // 修改医嘱执行状态 ：执行完成
	            longAdvice = hosSendAdviceDao.updateVAF(map);
	        }
	        map.put("TmpVAFzt", longAdvice);
	        // 参数
	        String date = DateUtils.getDateString(new Date());
	        map.put("aStr", "0000000000");
	        map.put("curDate", date);
	        map.put("curtime", " " + date.substring(11));
	        map.put("BBY31", date.subSequence(0, 11) + " 23:59:59.999");
	        map.put("eVAF38", map.get("lEndDate").toString());
	        long start3 = System.currentTimeMillis();
			log.info("============================updateVAF时间===========================:"+(start3-start2));
	        // 医疗项目类别
	        List tmpBDA = hosSendAdviceDao.getTempBDA(map);
	        

			map.put("tmpBDA", tmpBDA);
			// 检查药品是否过期 禁用
	        String disableMedical = checkDisableMedical(map);
	        if (disableMedical != null) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg(disableMedical + "----以上药品已过有效期或已禁用，不能继续使用");
	            cleantable(map);
	            return mo;
	        }
	        
	        // 医嘱计价
	        List tmpVBD = hosSendAdviceDao.queryTemVBD(map);
	        long start4 = System.currentTimeMillis();
			log.info("============================queryTemVBD时间===========================:"+(start4-start3));
	        map.put("tmpVBD", tmpVBD);
	        // 病人医嘱
	        int aMday = hosSendAdviceDao.getMDay(map);	
	       	        
	        List tempVAF = getkbmjzbs(map,aMday);
	        long start5 = System.currentTimeMillis();
			log.info("============================getkbmjzbs时间===========================:"+(start5-start4));
	        map.put("kbmjzbs", tempVAF);
	        map.put("kbmVBD", tmpVBD);
	        tmpVBD = getTempVBD(map, BCK1,aMday);
	        
	        long start6 = System.currentTimeMillis();
			log.info("============================getTempVBD时间===========================:"+(start6-start5));
			String vaf22 = hosSendAdviceDao.getPriceContent(map);
	        if (!"1".equals(params.get("gpara90").toString()) && vaf22 != null) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg(vaf22 + "--此医嘱没有计价内容，不能发送，请先联系管理员");
	            cleantable(map);
	            return mo;
	        }
	        // 费用明细	        
			map.put("tmpVBD", tmpVBD);
			List TmpVAJ = getTempVAJ(map);
			map.put("TmpVAJ", TmpVAJ);
	        // 费用单据
	        List tmpVAI = getTempVAI(map);
	        map.put("tmpVAI", tmpVAI);
	        TmpVAJ = getTempVAJ2(map);
	        map.put("TmpVAJ", TmpVAJ);
	        long start7 = System.currentTimeMillis();
			log.info("============================getTempVAJ2时间===========================:"+(start7-start6));
	        List TmpVAFzt = getTmpVAFzt2(map);
	        map.put("TmpVAFzt", TmpVAFzt);
	        // 抄送记录
	        long start8 = System.currentTimeMillis();
			log.info("============================getTmpVAFzt2时间===========================:"+(start8-start7));
	        List tmpVBI = getTempVBI2(map,aMday);
	        long start9 = System.currentTimeMillis();
			log.info("============================getTempVBI2时间===========================:"+(start9-start8));
	        
			map.put("tmpVBI", tmpVBI);
	        // 医嘱是否作废或停止
	        if (isDelete(map)) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg("已有医嘱被作废或停止，请返回重新选择过滤条件，在发送医嘱");
	            cleantable(map);
	            return mo;
	        }
	        // 是否已执行

	        if (isExecute(map)) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg("已有医嘱被作废或已执行，请返回重新选择过滤条件，再发送医嘱");
	            cleantable(map);
	            return mo;
	        }

	        // 药品是否禁售
	        String forbidden = isForbidden(map);
	        if (forbidden != null) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg(forbidden + "药房已经禁售");
	            cleantable(map);
	            return mo;
	        }
	        List<Map<String, Object>> ls = hosSendAdviceDao.getLimitStore1(map,1);
			if (ls != null && !ls.isEmpty()) {
				
				String msg = "[";
				for(int i=0;i<ls.size();i++){
					msg+=ls.get(i).get("bby05")+",";
				}
				msg+="]库存数量不足";
				mo.setResultMsg(msg);
				mo.setResult(StaticKeys.OH_FALSE);
				cleantable(map);
				return mo;
			}
	        long start10 = System.currentTimeMillis();
			log.info("============================isForbidden时间===========================:"+(start10-start9));
	        // 更新数据
	        insetOrUpdateTable(map);
	        long start11 = System.currentTimeMillis();
	        log.info("============================insetOrUpdateTable时间===========================:"+(start11-start10));
	        updateDPT1_DPTLog(map);
	        long start12 = System.currentTimeMillis();
	        log.info("============================updateDPT1_DPTLog时间===========================:"+(start12-start11)); 
	        cleantable(map);
		}		
		catch (Exception e)
		{	
		    e.printStackTrace();
		    mo.setResult(StaticKeys.OH_FALSE);
		    mo.setResultMsg(e.toString());
		}
		long start13 = System.currentTimeMillis();
		log.info("============================总用时===========================:"+(start13-start1));
		return mo;
	}
	//全部发送长嘱
	public ModelVo execNurseLongAdviceSendall(Map map) throws Exception {
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		long start1 = System.currentTimeMillis();
		map.put("tablename", String.valueOf(start1));
		log.info("============================开始时间===========================:"+(start1));
		try
		{
//		    JSONObject jsonVAF2 = JSONObject.fromObject(map.get("lXml").toString());
	        JSONObject jsonBCK1 = JSONObject.fromObject(map.get("lBCKxml").toString());
	        // 医嘱
	        List<Map<String, Object>> VAF2 = (List<Map<String, Object>>) map.get("vaf");
	        // 科室 部门
	        List<Map<String, Object>> BCK1 = JsonUtil.getList(jsonBCK1.getJSONArray("BCK1").toString());
	        map.put("BCK1", BCK1);
	        map.remove("lXml");
	        map.remove("lBCKxml");
	        // 药品库存量
//	      List amout = checkStoreNumber1(VAF2);
//	      if (amout != null && !amout.isEmpty()) {
//	          mo.setResult(StaticKeys.OH_FALSE);
//	          mo.setResultMsg(amout.toString() + "库存不足");
//	          return mo;
//	      }
	        // 只检索长嘱
	        List<Map<String, Object>> longAdvice = getLongAdvice(map, VAF2);
	        if (longAdvice.size() == 0) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            return mo;
	        }
	       
	        Map params = getParameters();
	        map.putAll(params);
	        
	        
	        // 长嘱信息	       
			map.put("TmpVAFzt", longAdvice);
			longAdvice = hosSendAdviceDao.getTempVAF2(map);
			long start2 = System.currentTimeMillis();
			log.info("============================getTempVAF2时间===========================:"+(start2-start1));
	        // 中、成药、西药
	        if (checkBAD04(map) && "1".equals(params.get("para140").toString())) {
	            // 修改医嘱执行状态 ：执行完成
	            longAdvice = hosSendAdviceDao.updateVAF(map);
	        }
	        map.put("TmpVAFzt", longAdvice);
	        // 参数
	        String date = DateUtils.getDateString(new Date());
	        map.put("aStr", "0000000000");
	        map.put("curDate", date);
	        map.put("curtime", " " + date.substring(11));
	        map.put("BBY31", date.subSequence(0, 11) + " 23:59:59.999");
	        map.put("eVAF38", map.get("lEndDate").toString());
	        long start3 = System.currentTimeMillis();
			log.info("============================updateVAF时间===========================:"+(start3-start2));
	        // 医疗项目类别
	        List tmpBDA = hosSendAdviceDao.getTempBDA(map);
	        

			map.put("tmpBDA", tmpBDA);
			// 检查药品是否过期 禁用
	        String disableMedical = checkDisableMedical(map);
	        if (disableMedical != null) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg(disableMedical + "----以上药品已过有效期或已禁用，不能继续使用");
	            return mo;
	        }
	        
	        // 医嘱计价
	        List tmpVBD = hosSendAdviceDao.queryTemVBD(map);
	        long start4 = System.currentTimeMillis();
			log.info("============================queryTemVBD时间===========================:"+(start4-start3));
	        map.put("tmpVBD", tmpVBD);
	        // 病人医嘱
	        int aMday = hosSendAdviceDao.getMDay(map);	
	       	        
	        List tempVAF = getkbmjzbs(map,aMday);
	        long start5 = System.currentTimeMillis();
			log.info("============================getkbmjzbs时间===========================:"+(start5-start4));
	        map.put("kbmjzbs", tempVAF);
	        map.put("kbmVBD", tmpVBD);
	        tmpVBD = getTempVBD(map, BCK1,aMday);
	        
	        long start6 = System.currentTimeMillis();
			log.info("============================getTempVBD时间===========================:"+(start6-start5));
			String vaf22 = hosSendAdviceDao.getPriceContent(map);
	        if (!"1".equals(params.get("gpara90").toString()) && vaf22 != null) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg(vaf22 + "--此医嘱没有计价内容，不能发送，请先联系管理员");
	            return mo;
	        }
	        // 费用明细	        
			map.put("tmpVBD", tmpVBD);
			List TmpVAJ = getTempVAJ(map);
			map.put("TmpVAJ", TmpVAJ);
	        // 费用单据
	        List tmpVAI = getTempVAI(map);
	        map.put("tmpVAI", tmpVAI);
	        TmpVAJ = getTempVAJ2(map);
	        map.put("TmpVAJ", TmpVAJ);
	        long start7 = System.currentTimeMillis();
			log.info("============================getTempVAJ2时间===========================:"+(start7-start6));
	        List TmpVAFzt = getTmpVAFzt2(map);
	        map.put("TmpVAFzt", TmpVAFzt);
	        // 抄送记录
	        long start8 = System.currentTimeMillis();
			log.info("============================getTmpVAFzt2时间===========================:"+(start8-start7));
	        List tmpVBI = getTempVBI2(map,aMday);
	        long start9 = System.currentTimeMillis();
			log.info("============================getTempVBI2时间===========================:"+(start9-start8));
	        
			map.put("tmpVBI", tmpVBI);
	        // 医嘱是否作废或停止
	        if (isDelete(map)) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg("已有医嘱被作废或停止，请返回重新选择过滤条件，在发送医嘱");
	            return mo;
	        }
	        // 是否已执行

	        if (isExecute(map)) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg("已有医嘱被作废或已执行，请返回重新选择过滤条件，再发送医嘱");
	            return mo;
	        }

	        // 药品是否禁售
	        String forbidden = isForbidden(map);
	        if (forbidden != null) {
	            mo.setResult(StaticKeys.OH_FALSE);
	            mo.setResultMsg(forbidden + "药品药房已经禁售");
	            cleantable(map);
	            return mo;
	        }
	        List<Map<String, Object>> ls = hosSendAdviceDao.getLimitStore1(map,1);
			if (ls != null && !ls.isEmpty()) {
				
				String msg = "[";
				for(int i=0;i<ls.size();i++){
					msg+=ls.get(i).get("bby05")+",";
				}
				msg+="]库存数量不足";
				mo.setResultMsg(msg);
				mo.setResult(StaticKeys.OH_FALSE);
				cleantable(map);
				return mo;
			}
	        long start10 = System.currentTimeMillis();
			log.info("============================isForbidden时间===========================:"+(start10-start9));
	        // 更新数据
	        insetOrUpdateTable(map);
	        long start11 = System.currentTimeMillis();
	        log.info("============================insetOrUpdateTable时间===========================:"+(start11-start10));
	        updateDPT1_DPTLog(map);
	        long start12 = System.currentTimeMillis();
	        log.info("============================updateDPT1_DPTLog时间===========================:"+(start12-start11));    
	        cleantable(map);
		}		
		catch (Exception e)
		{			
		    e.printStackTrace();
		    mo.setResult(StaticKeys.OH_FALSE);
		    mo.setResultMsg(e.toString());
		}
		long start13 = System.currentTimeMillis();
		log.info("============================总用时===========================:"+(start13-start1));
		return mo;
	}
	private void cleantable(Map map)throws Exception {
		hosSendAdviceDao.cleantable(map);
	}

	public List getTempVAJ(Map map) throws Exception {
		List<Map<String, Object>> tmpVAJ = hosSendAdviceDao.getTempVAJ(map);
		return tmpVAJ;
	}

	// 修改库存
	private void updateDPT1_DPTLog(Map map) throws Exception {
		List tmpDPT = hosSendAdviceDao.getTmpDPT(map);
		List tmpCLDPT = hosSendAdviceDao.getTmpCLDPT(map);
		String Para46 = getParameter(9999, 9999, 46);
		if (tmpDPT != null && tmpDPT.size() > 0)
		{
			//预处理库存--根据参数来判断9999-9999-46
			if("0".equals(Para46))
			{
				yaopinKucunBo.updateDpsAndDpt(tmpDPT);
			}
		}
		if (tmpCLDPT != null && tmpCLDPT.size() > 0)
		{
			//预处理库存--根据参数来判断9999-9999-46
			if("0".equals(Para46))
			{
				yaopinKucunBo.updateDpsAndDptCl(tmpCLDPT);
			}
		}
		
		// 添加库存日志
		insertDPTLog(map);
	}

	// 手术医嘱
	private void insertCBM2_VAT2_VAV2(Map map) throws Exception {
		/// 手术记录 手术情况
		List kbmVAT = null;
		List tmpVAV = null;
		// 是否存在手术计价信息
		if (isVaf(map)) {
			// 病人手术记录
			kbmVAT = getkbmVAT(map);
			// 病人手术情况
			map.put("kbmVAT", kbmVAT);
			tmpVAV = getTmpVAV(map);
			map.put("tmpVAV", tmpVAV);
		}
		map.put("kbmVAT", kbmVAT);
		map.put("tmpVAV", tmpVAV);
		// 新增手术单记录
		insertCBM2(map);
		// 手术记录
		insertVAT2(map);
		// 手术情况
		insertVAV2(map);
	}

	private void insetOrUpdateTable(Map map) throws Exception {
		// 新增收费单据 费用明细记录
		long start10 = System.currentTimeMillis();
		log.info("============================开始时间===========================:"+(start10));
		insertFee(map);
		// 修改医嘱
		updateVAF2(map);
		// 新增医嘱抄送记录

		insertVBI2(map);
		// 新增医嘱状态记录
		insertVBG2(map);
		long start11= System.currentTimeMillis();
		log.info("============================insertVBG2时间===========================:"+(start11-start10));
		// 计价明细
		List<Map<String, Object>> listp = checkperson(map);
		if (listp != null && !listp.isEmpty()) {
			for(int i=0;i<listp.size();i++){
				Map map3 = new HashMap<>();
				map3.put("tablename", map.get("tablename"));
				map3.put("vaa07",listp.get(i).get("vaa07"));
				Map n = isPriceContent(map3);
				if (n != null) {
					Object obj = n.get("bby01");
					if (obj != null) {// 存在收费项目
						String lAAG01 = obj.toString();// 护理级别
						
						map3.put("lAAG01", lAAG01);
						map3.put("VAE01", map3.get("vaa07"));
						map3.put("lBCE03", map.get("lBCE03"));
						map3.put("curDate", map.get("curDate"));
						map3.put("lBCE01", map.get("lBCE01"));
						if (lAAG01 != null) {
							// 查询病人是否存在护理
							if (isVAE1(map3)) {
								// 不存在新增变动记录
								List tmpVBO = getTmpVBO1(map3);
								if(tmpVBO.size()>0){
									map3.put("tmpVBO", tmpVBO);							
									insertVBO1(map3);
								}
								
							}
						}
					}
				}
			}
		}
			
		long start12= System.currentTimeMillis();
		log.info("============================计价明细时间===========================:"+(start12-start11));
		// 是否手术医嘱
		if (isOPS(map)) {
			insertCBM2_VAT2_VAV2(map);
		}
		long start13= System.currentTimeMillis();
		log.info("============================是否手术时间===========================:"+(start13-start12));
		// 新增医嘱执行通知

		insertVBX2(map);
		long start14= System.currentTimeMillis();
		log.info("============================新增医嘱执行通知时间===========================:"+(start14-start13));
		// 医嘱提醒状态修改
		updateVBZ2(map);
		long start15= System.currentTimeMillis();
		log.info("============================医嘱提醒状态修改时间===========================:"+(start15-start14));
		// 修改病人费用余额
		updateVBM(map);
		long start16= System.currentTimeMillis();
		log.info("============================修改病人费用余额时间===========================:"+(start16-start15));

	}

	public List getTmpVBO1(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getTmpVBO1(map);
		for (Map<String, Object> mm : ls) {
			mm.put("vbo01", getNewId.nextId());
		}
		return ls;
	}


	private void updateVBM(Map map) throws Exception {

		List<Map<String, Object>> m = getBlanceParams(map);
		if (m != null  && !m.isEmpty() ) {
			for(int i=0;i<m.size();i++){	
				Map<String, Object> m1 = m.get(i);
				Object vaa01 = m1.get("vaa01");
				Object aVAF38 = m1.get("avaf38");
				Object VAA07 = m1.get("vaa07");
				if (aVAF38 == null) {
					aVAF38 = 0;
				}
				if (VAA07 == null) {
					VAA07 = 0;
				}
				if (vaa01 != null && !"".equals(vaa01)) {
					if (Long.parseLong(vaa01.toString()) > 0) {
						HashMap hs = new HashMap();
						hs.put("lType", 2);
						hs.put("lBCK01", 0);
						hs.put("lVAA01", vaa01);
						hs.put("lMoney", aVAF38);
						hs.put("lVBM03", 2);
						hs.put("VAA07", VAA07);
						// 修改费用余额
						execNurseVBM1Update(hs);
					}
				}
			}
		}
		
	}

	private void updateVBZ2(Map map) throws Exception {
		hosSendAdviceDao.updateVBZ2(map);
	}

	private void insertVBX2(Map map) throws Exception {
		hosSendAdviceDao.insertVBX2(map);
	}

	private boolean isOPS(Map map) throws Exception {
		return hosSendAdviceDao.isOPS(map) > 0;
	}

	// 手术单记录
	private void insertCBM2(Map map) throws Exception {
		hosSendAdviceDao.insertCBM2(map);
	}

	// 病人手术记录
	private void insertVAT2(Map map) throws Exception {
		hosSendAdviceDao.insertVAT2(map);
	}

	// 手术情况记录
	private void insertVAV2(Map map) throws Exception {
		hosSendAdviceDao.insertVAV2(map);
	}

	private void insertVBO1(Map map) throws Exception {
		hosSendAdviceDao.insertVBO1(map);
	}

	private boolean isVAE1(Map map) throws Exception {
		return hosSendAdviceDao.isVAE1(map) == 0;
	}

	private Map isPriceContent(Map map) throws Exception {
		return hosSendAdviceDao.isPriceContent(map);
	}
	private List checkperson(Map map) throws Exception {
		return hosSendAdviceDao.checkperson(map);
	}
	private void insertVBG2(Map map) throws Exception {
		hosSendAdviceDao.insertVBG2(map);
	}

	private void updateVAF2(Map map) throws Exception {
		hosSendAdviceDao.updateVAF2(map);
	}

	private void insertVBI2(Map map) throws Exception {
		hosSendAdviceDao.insertVBI2(map);
	}

	private void insertFee(Map map) throws Exception {
		hosSendAdviceDao.insertFee(map);
	}

	// 余额修改
	private List getBlanceParams(Map map) throws Exception {
		return hosSendAdviceDao.getBlanceParams(map);
	}

	// 日志
	private void insertDPTLog(Map map) throws Exception {
		hosSendAdviceDao.insertDPTLog(map);
	}

	private boolean isDelete(Map map) throws Exception {
		return hosSendAdviceDao.isDelete(map) > 0;
	}

	private boolean isExecute(Map map) throws Exception {
		return hosSendAdviceDao.isExecute(map) > 0;
	}

	private String isForbidden(Map map) throws Exception {
		return hosSendAdviceDao.isForbidden(map);
	}

	private List getkbmVAT(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getkbmVAT(map);
		for (Map<String, Object> mm : ls) {
			mm.put("vat01", getNewId.nextId());
		}
		return ls;

	}

	private List getTmpVAV(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getTmpVAV(map);
		for (Map<String, Object> mm : ls) {
			mm.put("vav01", getNewId.nextId());
		}

		return ls;
	}

	private Map getParameters() throws Exception {
		return hosSendAdviceDao.getParameters();
	}

	private boolean checkBAD04(Map map) throws Exception {
		return hosSendAdviceDao.checkBAD04(map) > 0;
	}

	private String checkDisableMedical(Map map) throws Exception {
		return hosSendAdviceDao.checkDisableMedical(map);
	}

	private List getkbmjzbs(Map map,int aMday) throws Exception {
		List listAll = new ArrayList<>();
		List mmlist = new ArrayList<>();
		
		if (aMday >= 2) {
			int anum = 1;
			while (anum <= aMday) {
				 Map<String, Integer> mmp = new HashMap<String, Integer>();
				 mmp.put("anum", anum);
				 mmlist.add(mmp);
				 anum++;
			}			
		}
		map.put("aMday", mmlist);
		listAll = hosSendAdviceDao.getgetkbmjzbs2(map);

		return listAll;
	}

	private List getTempVBD(Map map, List BCK1,int aMday) throws Exception {
		List listAll = new ArrayList<>();
		List mmlist = new ArrayList<>();
		int bckint = BCK1.size();
		int count = hosSendAdviceDao.checkCount(map);
		map.put("bckcont", bckint);
		map.put("count", count);
		
		if (aMday >= 2) {
			int anum = 1;
			while (anum <= aMday) {
				 Map<String, Integer> mmp = new HashMap<String, Integer>();
				 mmp.put("anum", anum);
				 mmlist.add(mmp);
				 anum++;
			}		
		}
		map.put("aMday", mmlist);
		listAll = hosSendAdviceDao.getTempVBD2(map);
		return listAll;
	}

	private List getTempVAI(Map map) throws Exception {
		List<Map<String, Object>> tempVAI = hosSendAdviceDao.getTempVAI(map);
		List<Map<String, Object>> vaiid = new ArrayList<>();
		Integer vaicont =  GlobalVariable.IntegerIsNull(tempVAI.get(0).get("count")) ;
		for (int i=0;i<vaicont;i++) {
			Map vaimap = new HashMap<>();
			long id = getNewId.nextId();
			vaimap.put("vai01", id);
			vaimap.put("vai04", "CZ" + id);
			vaiid.add(vaimap);
		}
		return vaiid;
	}

	private List getTempVAJ2(Map map) throws Exception {
		List<Map<String, Object>> tempVAJ = hosSendAdviceDao.getTempVAJ2(map);
		List<Map<String, Object>> vajid = new ArrayList<>();
		Integer vajcont = GlobalVariable.IntegerIsNull(tempVAJ.get(0).get("count")) ;
		for (int i=0;i<vajcont;i++) {
			Map vajmap = new HashMap<>();
			long id = getNewId.nextId();
			vajmap.put("vaj01", id);
			vajmap.put("bag46", 0);
			vajmap.put("vbx01", UUID.randomUUID().toString().toUpperCase());
			vajid.add(vajmap);
		}
		return vajid;
	}

	private List getTmpVAFzt2(Map map) throws Exception {
		List<Map<String, Object>> TmpVAFzt = hosSendAdviceDao.getTmpVAFzt2(map);
		List<Map<String, Object>> vafztid = new ArrayList<>();
		Integer vafztcont = GlobalVariable.IntegerIsNull(TmpVAFzt.get(0).get("count")) ;
		for (int i=0;i<vafztcont;i++) {
			Map vafztmap = new HashMap<>();
			long id = getNewId.nextId();
			vafztmap.put("vbg01", id);
			vafztid.add(vafztmap);
		}
		return vafztid;
	}

	private List getTempVBI2(Map map,int aMday) throws Exception {
		List<Map<String, Object>> tempVBI = new ArrayList<>();
		List mmlist = new ArrayList<>();
		if (aMday >= 2) {
			int anum = 1;
			while (anum <= aMday) {
				 Map<String, Integer> mmp = new HashMap<String, Integer>();
				 mmp.put("anum", anum);
				 mmlist.add(mmp);
				 anum++;
			}
		}
		map.put("aMday", mmlist);
		tempVBI = hosSendAdviceDao.getTempVAI22(map);
		List<Map<String, Object>> vbiid = new ArrayList<>();
		Integer vbicont = GlobalVariable.IntegerIsNull(tempVBI.get(0).get("count")) ;
		for (int i=0;i<vbicont;i++) {
			Map vbimap = new HashMap<>();
			long id = getNewId.nextId();
			vbimap.put("vbi01", id);
			vbiid.add(vbimap);
		}
		return vbiid;
	}

	private boolean isVaf(Map map) throws Exception {
		return hosSendAdviceDao.isVaf(map) > 0;
	}

//	private List gettmp_DPT1(Map map) throws Exception {
//		return hosSendAdviceDao.gettmp_DPT1(map);
//	}
//
//	private List getDSK(List<Map<String, Object>> tmp_DPT1, String DSK_ID, double UpdQty) throws Exception {
//		double RefQty = 0;// 账面数量
//		double SellQty = 0;// 记账数量
//		double Quantity = 0;// 入库数量
//
//		if (tmp_DPT1 != null && tmp_DPT1.size() > 0) {
//			if (UpdQty > 0) {
//				for (Map<String, Object> map : tmp_DPT1) {
//					if (DSK_ID.equals(map.get("dsk_id"))) {
//						if (map != null) {
//							Object _refqty = map.get("refqty");
//							Object _sellqty = map.get("sellqty");
//							Object _quantity = map.get("quantity");
//							if (_refqty != null && !"".equals(_refqty)) {
//								RefQty = Double.parseDouble(_refqty.toString());
//							}
//							if (_sellqty != null && !"".equals(_sellqty)) {
//								SellQty = Double.parseDouble(_sellqty.toString());
//							}
//							if (_quantity != null && !"".equals(_quantity)) {
//								Quantity = Double.parseDouble(_quantity.toString());
//							}
//
//							if (UpdQty + SellQty <= RefQty) {
//
//								map.put("sellqty", SellQty + UpdQty);
//								map.put("quantity", Quantity - UpdQty);
//								UpdQty = 0;
//							} else {
//
//								map.put("sellqty", RefQty);
//								map.put("quantity", 0);
//								UpdQty = UpdQty - (RefQty - SellQty);
//							}
//
//						}
//
//					}
//				}
//
//				if (UpdQty != 0) {
//					for (Map<String, Object> map : tmp_DPT1) {
//						Object _refqty = map.get("refqty");
//						Object _sellqty = map.get("sellqty");
//						Object _quantity = map.get("quantity");
//						if (_refqty != null && !"".equals(_refqty)) {
//							RefQty = Double.parseDouble(_refqty.toString());
//						}
//						if (_sellqty != null && !"".equals(_sellqty)) {
//							SellQty = Double.parseDouble(_sellqty.toString());
//						}
//						if (_quantity != null && !"".equals(_quantity)) {
//							Quantity = Double.parseDouble(_quantity.toString());
//						}
//						if (UpdQty + SellQty <= RefQty) {
//
//							map.put("sellqty", SellQty + UpdQty);
//							map.put("quantity", Quantity - UpdQty);
//							UpdQty = 0;
//							break;
//						}
//						UpdQty = UpdQty - (RefQty - SellQty);
//						map.put("sellqty", RefQty);
//
//					}
//
//				}
//			} else if (UpdQty < 0) {
//
//				for (Map<String, Object> map : tmp_DPT1) {
//					if (DSK_ID.equals(map.get("dsk_id"))) {
//						Object _refqty = map.get("refqty");
//						Object _sellqty = map.get("sellqty");
//						Object _quantity = map.get("quantity");
//						if (_refqty != null && !"".equals(_refqty)) {
//							RefQty = Double.parseDouble(_refqty.toString());
//						}
//						if (_sellqty != null && !"".equals(_sellqty)) {
//							SellQty = Double.parseDouble(_sellqty.toString());
//						}
//						if (_quantity != null && !"".equals(_quantity)) {
//							Quantity = Double.parseDouble(_quantity.toString());
//						}
//
//						if (UpdQty + SellQty >= 0) {
//
//							map.put("sellqty", SellQty + UpdQty);
//							map.put("quantity", Quantity - UpdQty);
//						} else {
//							UpdQty = UpdQty + SellQty;
//							map.put("sellqty", 0);
//							map.put("quantity", RefQty);
//						}
//
//					}
//				}
//				if (UpdQty != 0) {
//					for (Map<String, Object> map : tmp_DPT1) {
//						Object _refqty = map.get("refqty");
//						Object _sellqty = map.get("sellqty");
//						Object _quantity = map.get("quantity");
//						if (_refqty != null && !"".equals(_refqty)) {
//							RefQty = Double.parseDouble(_refqty.toString());
//						}
//						if (_sellqty != null && !"".equals(_sellqty)) {
//							SellQty = Double.parseDouble(_sellqty.toString());
//						}
//						if (_quantity != null && !"".equals(_quantity)) {
//							Quantity = Double.parseDouble(_quantity.toString());
//						}
//						if (UpdQty + SellQty >= 0) {
//							UpdQty = 0;
//							map.put("sellqty", SellQty + UpdQty);
//							map.put("quantity", Quantity - UpdQty);
//							break;
//						}
//						UpdQty = UpdQty + SellQty;
//						map.put("sellqty", 0);
//						map.put("quantity", RefQty);
//					}
//				}
//			}
//		}
//		return tmp_DPT1;
//	}
//
//	private void updateDPT1(Map map) throws Exception {
//		hosSendAdviceDao.updateDPT1(map);
//	}

	public List execDPSDPT1Update(List list)throws Exception{
		Double updQty =0d;
		Double refQty=0d;
		Double sellQty=0d,quantity=0d;
		List<Map<String,Object>> rList = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < list.size(); i++) {
			Map m = (Map) list.get(i);
			m.put("ROW_NAME", "DSK_ID");
			m.put("dsk01", MapUtils.getLongValue(m,"dsk01",0l));
			Map map = getRefQtyAndSellQty(m);
			if(map==null)
			{
				continue;
			}
			m.put("updqty", m.get("vaj25"));
			String RefQty = map.get("refqty")==null||"".equals(map.get("refqty").toString())?"0":map.get("refqty").toString();
			String SellQty = map.get("refqty")==null||"".equals(map.get("sellqty").toString())?"0":map.get("sellqty").toString();
			String Quantity = map.get("quantity")==null||"".equals(map.get("quantity").toString())?"0":map.get("quantity").toString();
			refQty = Double.parseDouble(RefQty);
			sellQty = Double.parseDouble(SellQty);
			quantity = Double.parseDouble(Quantity);
			String UpdQty=m.get("updqty")==null||"".equals(m.get("updqty").toString())?"0":m.get("updqty").toString();
			updQty = Double.parseDouble(UpdQty);
			if(updQty>0){//增加预扣数
				if(updQty + sellQty <= refQty){
					map.put("SELLQTY", sellQty +updQty);
					map.put("Quantity", quantity - updQty);
					rList=select_tmp_DPT1_param1(m);
					updQty=0d;
				}else{

					rList=select_tmp_DPT1_param2(m);
					updQty = updQty - (refQty - sellQty);
				}
				m.put("updqty", updQty);

				if(updQty!=0){
					m.put("list", rList);
					Map maps = getMaxAndMin(m);
					int max = Integer.parseInt(maps.get("itatol").toString());
					int min = Integer.parseInt(maps.get("icount").toString());
					for (;min <= max; min++) {
						m.put("list", rList);
						m.put("ROW_NAME", "XROWNUM");
						m.put("DSK_ID", min);
						map = getRefQtyAndSellQty1(m);
						if (map!=null)
						{
							RefQty = map.get("refqty") == null || "".equals(map.get("refqty").toString()) ? "0"
									: map.get("refqty").toString();
							SellQty = map.get("refqty") == null || "".equals(map.get("sellqty").toString()) ? "0"
									: map.get("sellqty").toString();
							refQty = Double.parseDouble(RefQty);
							sellQty = Double.parseDouble(SellQty);
							if (updQty + sellQty <= refQty)
							{
								rList = select_tmp_DPT1_param3(m);

								updQty = 0d;
								m.put("updqty", updQty);
								break;
							}
							rList = select_tmp_DPT1_param4(m);
							updQty = updQty - (refQty - sellQty);
							m.put("updqty", updQty);
						}
					}
				}
			}else if(updQty<0){//减少预扣数
				if(updQty + sellQty >= 0){

					rList = select_tmp_DPT1_param5(m);
					updQty=0d;
				}else{

					rList = select_tmp_DPT1_param6(m);
					updQty = updQty + sellQty;
				}
				m.put("updqty", updQty);
				if(updQty!=0){
					m.put("list", rList);
					Map maps = getMaxAndMin(m);
					int max = Integer.parseInt(maps.get("itatol").toString());
					int min = Integer.parseInt(maps.get("icount").toString());
					for (; min <= max; min++) {
						m.put("list", rList);
						m.put("ROW_NAME", "XROWNUM");
						m.put("DSK_ID", min);
						map = getRefQtyAndSellQty1(m);
						RefQty = map.get("refqty")==null||"".equals(map.get("refqty").toString())?"0":map.get("refqty").toString();
						SellQty = map.get("refqty")==null||"".equals(map.get("sellqty").toString())?"0":map.get("sellqty").toString();
						refQty = Double.parseDouble(RefQty);
						sellQty = Double.parseDouble(SellQty);
						if(updQty + sellQty >= 0){

							rList = select_tmp_DPT1_param7(m);
							updQty = 0d;
							m.put("updqty", updQty);
							break;
						}
						rList = select_tmp_DPT1_param8(m);
						updQty = updQty + sellQty;
						m.put("updqty", updQty);
					}
				}
			}
		}
		return rList;
	}
	//材料库存处理
	public List execMPSMPT1Update(List list)throws Exception{
		Double updQty =0d;
		Double refQty=0d;
		Double sellQty=0d,quantity=0d;
		List<Map<String,Object>> rList = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < list.size(); i++) {
			Map m = (Map) list.get(i);
			m.put("ROW_NAME", "MSK_ID");
			m.put("msk01", MapUtils.getLongValue(m,"dsk01",0l));
			Map map = getRefQtyAndSellQtywz(m);
			if(map==null)
			{
				continue;
			}
			m.put("updqty", m.get("vaj25"));
			String RefQty = map.get("refqty")==null||"".equals(map.get("refqty").toString())?"0":map.get("refqty").toString();
			String SellQty = map.get("refqty")==null||"".equals(map.get("sellqty").toString())?"0":map.get("sellqty").toString();
			String Quantity = map.get("quantity")==null||"".equals(map.get("quantity").toString())?"0":map.get("quantity").toString();
			refQty = Double.parseDouble(RefQty);
			sellQty = Double.parseDouble(SellQty);
			quantity = Double.parseDouble(Quantity);
			String UpdQty=m.get("updqty")==null||"".equals(m.get("updqty").toString())?"0":m.get("updqty").toString();
			updQty = Double.parseDouble(UpdQty);
			if(updQty>0){//增加预扣数
				if(updQty + sellQty <= refQty){
					map.put("SELLQTY", sellQty +updQty);
					map.put("Quantity", quantity - updQty);
					rList=select_tmp_DPT1_param1wz(m);
					updQty=0d;
				}else{

					rList=select_tmp_DPT1_param2wz(m);
					updQty = updQty - (refQty - sellQty);
				}
				m.put("updqty", updQty);

				if(updQty!=0){
					m.put("list", rList);
					Map maps = getMaxAndMinwz(m);
					int max = Integer.parseInt(maps.get("itatol").toString());
					int min = Integer.parseInt(maps.get("icount").toString());
					for (;min <= max; min++) {
						m.put("list", rList);
						m.put("ROW_NAME", "XROWNUM");
						m.put("MSK_ID", min);
						map = getRefQtyAndSellQty1wz(m);
						if (map!=null)
						{
							RefQty = map.get("refqty") == null || "".equals(map.get("refqty").toString()) ? "0"
									: map.get("refqty").toString();
							SellQty = map.get("refqty") == null || "".equals(map.get("sellqty").toString()) ? "0"
									: map.get("sellqty").toString();
							refQty = Double.parseDouble(RefQty);
							sellQty = Double.parseDouble(SellQty);
							if (updQty + sellQty <= refQty)
							{
								rList = select_tmp_DPT1_param3wz(m);

								updQty = 0d;
								m.put("updqty", updQty);
								break;
							}
							rList = select_tmp_DPT1_param4wz(m);
							updQty = updQty - (refQty - sellQty);
							m.put("updqty", updQty);
						}
					}
				}
			}else if(updQty<0){//减少预扣数
				if(updQty + sellQty >= 0){

					rList = select_tmp_DPT1_param5wz(m);
					updQty=0d;
				}else{

					rList = select_tmp_DPT1_param6wz(m);
					updQty = updQty + sellQty;
				}
				m.put("updqty", updQty);
				if(updQty!=0){
					m.put("list", rList);
					Map maps = getMaxAndMinwz(m);
					int max = Integer.parseInt(maps.get("itatol").toString());
					int min = Integer.parseInt(maps.get("icount").toString());
					for (; min <= max; min++) {
						m.put("list", rList);
						m.put("ROW_NAME", "XROWNUM");
						m.put("MSK_ID", min);
						map = getRefQtyAndSellQty1wz(m);
						RefQty = map.get("refqty")==null||"".equals(map.get("refqty").toString())?"0":map.get("refqty").toString();
						SellQty = map.get("refqty")==null||"".equals(map.get("sellqty").toString())?"0":map.get("sellqty").toString();
						refQty = Double.parseDouble(RefQty);
						sellQty = Double.parseDouble(SellQty);
						if(updQty + sellQty >= 0){

							rList = select_tmp_DPT1_param7wz(m);
							updQty = 0d;
							m.put("updqty", updQty);
							break;
						}
						rList = select_tmp_DPT1_param8wz(m);
						updQty = updQty + sellQty;
						m.put("updqty", updQty);
					}
				}
			}
		}
		return rList;
	}
	//---物资库存处理---
	private Map getRefQtyAndSellQtywz(Map map)throws Exception{
		return hosSendAdviceDao.getRefQtyAndSellQtywz(map);
	}
	private Map getRefQtyAndSellQty1wz(Map map)throws Exception{
		return hosSendAdviceDao.getRefQtyAndSellQty1wz(map);
	}
	private List select_tmp_DPT1_param1wz(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param1wz(map);
	}
	private List select_tmp_DPT1_param2wz(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param2wz(map);
	}
	private List select_tmp_DPT1_param3wz(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param3wz(map);
	}
	private List select_tmp_DPT1_param4wz(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param4wz(map);
	}
	private List select_tmp_DPT1_param5wz(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param5wz(map);
	}
	private List select_tmp_DPT1_param6wz(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param6wz(map);
	}
	private List select_tmp_DPT1_param7wz(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param7wz(map);
	}
	private List select_tmp_DPT1_param8wz(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param8wz(map);
	}
	private Map getMaxAndMinwz(Map map)throws Exception{
		return hosSendAdviceDao.getMaxAndMinwz(map);
	}
	//---物资库存处理结束---
	//---库存处理---
	private Map getRefQtyAndSellQty(Map map)throws Exception{
		return hosSendAdviceDao.getRefQtyAndSellQty(map);
	}
	private Map getRefQtyAndSellQty1(Map map)throws Exception{
		return hosSendAdviceDao.getRefQtyAndSellQty1(map);
	}
	private List select_tmp_DPT1_param1(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param1(map);
	}
	private List select_tmp_DPT1_param2(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param2(map);
	}
	private List select_tmp_DPT1_param3(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param3(map);
	}
	private List select_tmp_DPT1_param4(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param4(map);
	}
	private List select_tmp_DPT1_param5(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param5(map);
	}
	private List select_tmp_DPT1_param6(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param6(map);
	}
	private List select_tmp_DPT1_param7(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param7(map);
	}
	private List select_tmp_DPT1_param8(Map map)throws Exception{
		return hosSendAdviceDao.select_tmp_DPT1_param8(map);
	}
	private Map getMaxAndMin(Map map)throws Exception{
		return hosSendAdviceDao.getMaxAndMin(map);
	}
	//---库存处理结束---


	public ModelVo treatmentType() throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosSendAdviceDao.treatmentType();

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;

	}

	public ModelVo currentOffice() throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			list = hosSendAdviceDao.currentOffice();

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;

	}

	public ModelVo execNurseAdviceSendOther(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			String lxml = map.get("lxml").toString();
			String lBCKxml = map.get("lBCKxml").toString();
			JSONObject jsonVAF2 = JSONObject.fromObject(lxml);
			JSONObject jsonBCK1 = JSONObject.fromObject(lBCKxml);
			List<Map<String, Object>> VAE1 = JsonUtil.parserToList(jsonVAF2.getJSONArray("VAE1"));
			List<Map<String, Object>> BDA1 = JsonUtil.parserToList(jsonVAF2.getJSONArray("BDA1"));
			List<Map<String, Object>> BCK1 = JsonUtil.parserToList(jsonBCK1.getJSONArray("BCK1"));
			if (VAE1.isEmpty() || BDA1.isEmpty() || BCK1.isEmpty()) {
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("提交的数据不能为空");
				return mo;
			}
         
			map.put("VAE1", VAE1);
			map.put("BDA1", BDA1);
			map.put("BCK1", BCK1);

			String lEDate = DateUtils.addInteger(map.get("lEndDate").toString(), Calendar.DATE, 1);
			map.put("lEDate", lEDate);
			map.put("lVAF38", map.get("lEndDate").toString());

			list = hosSendAdviceDao.nurseAdviceSendOther(map);

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}

	public ModelVo execNurseAdviceSendOtherPrice(Map map) throws Exception {
		ModelVo mo = new ModelVo();
		List list = null;
		try {
			String lxml = map.get("lxml").toString();
			String lBCKxml = map.get("lBCKxml").toString();
			JSONObject jsonVAF2 = JSONObject.fromObject(lxml);
			JSONObject jsonBCK1 = JSONObject.fromObject(lBCKxml);

			List<Map<String, Object>> VAE1 = JsonUtil.parserToList(jsonVAF2.getJSONArray("VAE1"));
			List<Map<String, Object>> BDA1 = JsonUtil.parserToList(jsonVAF2.getJSONArray("BDA1"));
			List<Map<String, Object>> BCK1 = JsonUtil.parserToList(jsonBCK1.getJSONArray("BCK1"));
			if (VAE1.isEmpty() || BDA1.isEmpty() || BCK1.isEmpty()) {
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("提交的数据不能为空");
				return mo;
			}

			map.put("VAE1", VAE1);
			map.put("BDA1", BDA1);
			map.put("BCK1", BCK1);
			map.remove("lxml");
			map.remove("lBCKxml");
			String lEDate = DateUtils.addInteger(map.get("lEndDate").toString(), Calendar.DATE, 1);
			map.put("lEDate", lEDate);
			map.put("lVAF38", map.get("lEndDate").toString());
			Map m = hosSendAdviceDao.getPara209_para262();
			map.putAll(m);

			List BDAOther = hosSendAdviceDao.getBDAother(map);
			map.put("BDAOther", BDAOther);

			List<Map<String, Object>> temp_adv = hosSendAdviceDao.getTmp_AdvCur(map);
			List adv_price = getAdv_price(map, temp_adv);

			map.put("adv_price", adv_price);
			list = hosSendAdviceDao.nurseAdviceSendOtherPrice(map);

		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;

	}

	public List getAdv_price(Map map, List<Map<String, Object>> temp_adv) throws Exception {
		int length = temp_adv.size();
		List adv_price = new ArrayList();
		long lVAFID = 0l, lVAFIDA = 0l;
		String lBDA01 = "";
		int VAF04 = 0;
		List ls = null;
		if (length > 0) {
			for (Map<String, Object> mm : temp_adv) {
				lVAFID = Long.parseLong(mm.get("vaf01").toString());
				lVAFIDA = Long.parseLong(mm.get("vaf01a").toString());
				lBDA01 = mm.get("bda01").toString();
				VAF04 = Integer.parseInt(mm.get("vaf04").toString());

				map.put("lVAFID", lVAFID);
				map.put("lVAFIDA", lVAFIDA);
				map.put("lBDA01", lBDA01);
				map.put("VAF04", VAF04);
				if ("S".equals(lBDA01)) {
					if (2 == VAF04) {
						ls = hosSendAdviceDao.getAdv_Price_S2(map);

					} else {

						ls = hosSendAdviceDao.getAdv_Price_S1(map);

					}
				} else {
					if ("E".equals(lBDA01)) {
						if (2 == VAF04) {
							ls = hosSendAdviceDao.getAdv_Price_E2(map);
						} else {
							ls = hosSendAdviceDao.getAdv_Price_E1(map);

						}

					} else {
						if (2 == VAF04) {
							ls = hosSendAdviceDao.getAdv_Price_Z2(map);

						} else {

							ls = hosSendAdviceDao.getAdv_Price_Z1(map);

						}

					}

				}

				adv_price.addAll(ls);

			}

		}

		return adv_price;
	}

	//Nurse_Temp_Advice_Send 临嘱发送
	public ModelVo execNurseTempAdviceSend(Map map) throws Exception {
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		String lSend = map.get("lSend").toString();
		String lxml = map.get("lXml").toString();
		String lBCKxml = map.get("lBCKxml").toString();
		JSONObject jsonVAF2 = JSONObject.fromObject(lxml);
		JSONObject jsonBCK1 = JSONObject.fromObject(lBCKxml);
		List<Map<String, Object>> VAF2 = JsonUtil.getList(jsonVAF2.getJSONArray("VAF1").toString());
		//检查vbm2表
        for (Map<String, Object> m : VAF2) {
			Map<String, Object> vaf2map = hosDoctorAdviceBo.getvaf2(m);
			if(MapUtils.getString(vaf2map, "vaf30").equals("09")){
				int size = hosDoctorAdviceBo.selectvbm1(vaf2map);
				if(size==0){
					vo.setResult(StaticKeys.OH_FALSE);
					vo.setResultMsg(MapUtils.getString(vaf2map,"vaf22")+" 的病人医嘱单数据没有写入,请删除此条医嘱并重新开这条医嘱!");
					return vo;
				}
			}
		}
		List<Map<String, Object>> BCK1 = JsonUtil.getList(jsonBCK1.getJSONArray("BCK1").toString());
		map.put("IsNurse", 0);
		if (hosSendAdviceDao.checkNurse() > 0) {
			map.put("IsNurse", 1);
		}
		map.put("aStr", "0000000000");
		map.put("aDate", DateUtils.getDateString(new Date()));
		map.put("EndDate", DateUtils.getDateStr(new Date()) + " 23:59:59.999");
		List<Map<String, Object>> tempAdvice = new ArrayList<>();
		if (VAF2 != null && VAF2.size() > 0) {
			for (Map<String, Object> m : VAF2) {
				if (m.containsKey("vaf11")) {
					if ("2".equals(m.get("vaf11").toString())) {
						tempAdvice.add(m);
					}
				}
			}
		}
		map.put("VAF2", tempAdvice);
		map.put("BCK1", BCK1);
		Map map1 = hosSendAdviceDao.params_Para37_Para53_Para140_gpara91();
		map.putAll(map1);
		List tmpvafzt = hosSendAdviceDao.getTempAdvice(map);
		map.put("TmpVAFzt", tmpvafzt);
		List tmpVBD = getTempVBD(map);
		map.put("VBD1", tmpVBD);
		// 药品是否过期
		String sdate = hosSendAdviceDao.checkDate(map);
		if (sdate != null) {
			vo.setResultMsg("[" + sdate + "]此药品有效期已过，不能继续使用");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
//		// 计价内容
		String content = hosSendAdviceDao.priceContent(map);
		if (content != null) {
			vo.setResultMsg("[" + content + "]此医嘱没有计价内容，不能发送，请先联系管理员");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		List tmpVAJ = getVAJ2(map);
		map.put("tmpVAJ", tmpVAJ);

		List tmpVAI = getVBI2(map);
		map.put("tmpVAI", tmpVAI);

		tmpVAJ = tempVAJ(map);
		map.put("tmpVAJ", tmpVAJ);

		tmpvafzt = tmpVAFzt(map);
		map.put("TmpVAFzt", tmpvafzt);

		tmpVAI = tmpVAI(map);
		map.put("tmpVAI", tmpVAI);

		// 医嘱作废
		if (hosSendAdviceDao.isDestory(map) > 0) {
			vo.setResultMsg("已有医嘱被作废或停止，请返回重新选择过滤条件，再执行医嘱");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
//		// 药品禁售
		String forbid = hosSendAdviceDao.isForbid(map);
		if (forbid != null) {
			vo.setResultMsg("[" + forbid + "]已经禁售");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		List<Map<String, Object>> ls = hosSendAdviceDao.getLimitStore1(map,0);
		if (ls != null && !ls.isEmpty()) {

			String msg = "[";
			for(int i=0;i<ls.size();i++){
				msg+=ls.get(i).get("bby05")+",";
			}
			msg+="]库存数量不足";
			vo.setResultMsg(msg);
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		// 是否手术医嘱
		List tmpVAT = null;
		List tmpVAV = null;
		if (hosSendAdviceDao.checkOPS(map) > 0) {
			tmpVAT = getTmpVAT(map);
			map.put("tmpVAT",tmpVAT);
			tmpVAV = getTmpVAV2(map);

		}
		map.put("tmpVAT", tmpVAT);
		map.put("tmpVAV", tmpVAV);
		// 保存费用明细
		if (tmpVAJ.size() != 0) {
			insertVAI2_VAJ2_VAJT(map);
		}
		// 修改医嘱状态 添加医嘱抄送
		updateVAF2_VBI2(map);
		// 添加计价信息
		if (tmpVBD.size() != 0 && "1".equals(lSend)) {
			insertVBD2(map);
		}
		// 添加医嘱状态
		addVBG2(map);
		// 手术医嘱
		if (isOperation(map)) {
			insertCBM2_VAT1_VAV1(map);
		}
		// 护理记录
		if (isCare(map)) {
			List tmpVBO = getVBO1(map);
			map.put("tmpVBO", tmpVBO);
			addVBO1(map);

		}
		// 添加医嘱通知
		updateVBX1_VBZ1(map);
		// 病人余额修改
		updateVBM2(map);
		// 库存处理
		List tmpDPT = getDEPT(map);
		List tmpCLDPT = getCLDEPT(map);
		String Para46 = getParameter(9999, 9999, 46);
		if (tmpDPT != null && tmpDPT.size() > 0)
		{
			//预处理库存--根据参数来判断9999-9999-46
			if("0".equals(Para46))
			{
				yaopinKucunBo.updateDpsAndDpt(tmpDPT);
			}
		}
		if (tmpCLDPT != null && tmpCLDPT.size() > 0)
		{
			//预处理库存--根据参数来判断9999-9999-46
			if("0".equals(Para46))
			{
				yaopinKucunBo.updateDpsAndDptCl(tmpCLDPT);
			}
		}
		return vo;

	}
	//全部发送临嘱
	public ModelVo execNurseTempAdviceSendall(Map map) throws Exception {
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		String lSend = map.get("lSend").toString();
//		String lxml = map.get("lXml").toString();
		String lBCKxml = map.get("lBCKxml").toString();
//		JSONObject jsonVAF2 = JSONObject.fromObject(lxml);
		JSONObject jsonBCK1 = JSONObject.fromObject(lBCKxml);
		 List<Map<String, Object>> VAF2 = (List<Map<String, Object>>) map.get("vaf");
		//检查vbm2表
        for (Map<String, Object> m : VAF2) {
			Map<String, Object> vaf2map = hosDoctorAdviceBo.getvaf2(m);
			if(MapUtils.getString(vaf2map, "vaf30").equals("09")){
				int size = hosDoctorAdviceBo.selectvbm1(vaf2map);
				if(size==0){
					vo.setResult(StaticKeys.OH_FALSE);
					vo.setResultMsg(MapUtils.getString(vaf2map,"vaf22")+" 的病人医嘱单数据没有写入,请删除此条医嘱并重新开这条医嘱!");
					return vo;
				}
			}
		}
		List<Map<String, Object>> BCK1 = JsonUtil.getList(jsonBCK1.getJSONArray("BCK1").toString());
		map.put("IsNurse", 0);
		if (hosSendAdviceDao.checkNurse() > 0) {
			map.put("IsNurse", 1);
		}
		map.put("aStr", "0000000000");
		map.put("aDate", DateUtils.getDateString(new Date()));
		map.put("EndDate", DateUtils.getDateStr(new Date()) + " 23:59:59.999");
		List<Map<String, Object>> tempAdvice = new ArrayList<>();
		if (VAF2 != null && VAF2.size() > 0) {
			for (Map<String, Object> m : VAF2) {
				if (m.containsKey("vaf11")) {
					if ("2".equals(m.get("vaf11").toString())) {
						tempAdvice.add(m);
					}
				}
			}
		}
		map.put("VAF2", tempAdvice);
		map.put("BCK1", BCK1);
		Map map1 = hosSendAdviceDao.params_Para37_Para53_Para140_gpara91();
		map.putAll(map1);
		List tmpvafzt = hosSendAdviceDao.getTempAdvice(map);
		map.put("TmpVAFzt", tmpvafzt);
		List tmpVBD = getTempVBD(map);
		map.put("VBD1", tmpVBD);
		// 药品是否过期
		String sdate = hosSendAdviceDao.checkDate(map);
		if (sdate != null) {
			vo.setResultMsg("[" + sdate + "]此药品有效期已过，不能继续使用");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		// 计价内容
		String content = hosSendAdviceDao.priceContent(map);
		if (content != null) {
			vo.setResultMsg("[" + content + "]此医嘱没有计价内容，不能发送，请先联系管理员");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		List tmpVAJ = getVAJ2(map);
		map.put("tmpVAJ", tmpVAJ);

		List tmpVAI = getVBI2(map);
		map.put("tmpVAI", tmpVAI);

		tmpVAJ = tempVAJ(map);
		map.put("tmpVAJ", tmpVAJ);

		tmpvafzt = tmpVAFzt(map);
		map.put("TmpVAFzt", tmpvafzt);

		tmpVAI = tmpVAI(map);
		map.put("tmpVAI", tmpVAI);

		// 医嘱作废
		if (hosSendAdviceDao.isDestory(map) > 0) {
			vo.setResultMsg("已有医嘱被作废或停止，请返回重新选择过滤条件，再执行医嘱");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		// 药品禁售
		String forbid = hosSendAdviceDao.isForbid(map);
		if (forbid != null) {
			vo.setResultMsg("[" + forbid + "]已经禁售");
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		List<Map<String, Object>> ls = hosSendAdviceDao.getLimitStore1(map,0);
		if (ls != null && !ls.isEmpty()) {
			
			String msg = "[";
			for(int i=0;i<ls.size();i++){
				msg+=ls.get(i).get("bby05")+",";
			}
			msg+="]库存数量不足";
			vo.setResultMsg(msg);
			vo.setResult(StaticKeys.OH_FALSE);
			return vo;
		}
		// 是否手术医嘱
		List tmpVAT = null;
		List tmpVAV = null;
		if (hosSendAdviceDao.checkOPS(map) > 0) {
			tmpVAT = getTmpVAT(map);
			map.put("tmpVAT",tmpVAT);
			tmpVAV = getTmpVAV2(map);

		}
		map.put("tmpVAT", tmpVAT);
		map.put("tmpVAV", tmpVAV);
		// 保存费用明细
		if (tmpVAJ.size() != 0) {
			insertVAI2_VAJ2_VAJT(map);
		}
		// 修改医嘱状态 添加医嘱抄送
		updateVAF2_VBI2(map);
		// 添加计价信息
		if (tmpVBD.size() != 0 && "1".equals(lSend)) {
			insertVBD2(map);
		}
		// 添加医嘱状态
		addVBG2(map);
		// 手术医嘱
		if (isOperation(map)) {
			insertCBM2_VAT1_VAV1(map);
		}
		// 护理记录
		if (isCare(map)) {
			List tmpVBO = getVBO1(map);
			map.put("tmpVBO", tmpVBO);
			addVBO1(map);

		}
		// 添加医嘱通知
		updateVBX1_VBZ1(map);
		// 病人余额修改
		updateVBM2(map);
		// 库存处理
		List tmpDPT = getDEPT(map);
		List tmpCLDPT = getCLDEPT(map);
		String Para46 = getParameter(9999, 9999, 46);
		if (tmpDPT != null && tmpDPT.size() > 0)
		{
			//预处理库存--根据参数来判断9999-9999-46
			if("0".equals(Para46))
			{
				yaopinKucunBo.updateDpsAndDpt(tmpDPT);
			}
		}
		if (tmpCLDPT != null && tmpCLDPT.size() > 0)
		{
			//预处理库存--根据参数来判断9999-9999-46
			if("0".equals(Para46))
			{
				yaopinKucunBo.updateDpsAndDptCl(tmpCLDPT);
			}
		}

		return vo;

	}
	private List getDEPT(Map map) throws Exception {
		return hosSendAdviceDao.getDEPT(map);
	}
	private List getCLDEPT(Map map) throws Exception {
		return hosSendAdviceDao.getCLDEPT(map);
	}

	private void updateVBM2(Map map) throws Exception {
		List<Map<String, Object>> map2 = getVAA01_VAA07_aVAF38(map);
		if (map2 != null && !map2.isEmpty()) {
			for(int i=0;i<map2.size();i++){	
				Map<String, Object> map1 = map2.get(i);
				Object obj_vaa01 = map1.get("vaa01");
				Object obj_VAA07 = map1.get("vaa07");
				Object obj_aVAF38 = map1.get("avaf38");
				if (obj_vaa01 != null && !"".equals(obj_vaa01)) {
					long vaa01 = Long.parseLong(obj_vaa01.toString());
					long vaa07 = 0;
					double aVAF38 = 0d;
					if (obj_VAA07 != null) {
						vaa07 = Long.parseLong(obj_VAA07.toString());
					}
					if (obj_aVAF38 != null) {
						aVAF38 = Double.parseDouble(obj_aVAF38.toString());
					}
					if (vaa01 > 0) {
						HashMap hs = new HashMap();
						hs.put("lType", 2);
						hs.put("lBCK01", 0);
						hs.put("lVAA01", vaa01);
						hs.put("lMoney", aVAF38);
						hs.put("lVBM03", 2);
						hs.put("VAA07", vaa07);
						// 修改费用余额
						execNurseVBM1Update(hs);
					}
	
				}
			}
		}
	}

	private List getVAA01_VAA07_aVAF38(Map map) throws Exception {
		return hosSendAdviceDao.getVAA01_VAA07_aVAF38(map);
	}

	private void updateVBX1_VBZ1(Map map) throws Exception {
		hosSendAdviceDao.updateVBX1_VBZ1(map);
	}

	private void addVBO1(Map map) throws Exception {
		hosSendAdviceDao.addVBO1(map);
	}

	private List getVBO1(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getVBO1(map);
		for (Map<String, Object> mm : ls) {
			long newID = getNewId.nextId();
			mm.put("vbo01", newID);
		}
		return ls;
	}

	private boolean isCare(Map map) throws Exception {
		return hosSendAdviceDao.isCare(map) > 0;
	}

	private boolean isOperation(Map map) throws Exception {
		return hosSendAdviceDao.isOperation(map) > 0;
	}

	private void insertCBM2_VAT1_VAV1(Map map) throws Exception {
		hosSendAdviceDao.insertCBM2_VAT1_VAV1(map);
	}

	private void addVBG2(Map map) throws Exception {
		hosSendAdviceDao.addVBG2(map);
	}

	private void insertVBD2(Map map) throws Exception {
		hosSendAdviceDao.insertVBD2(map);
	}

	private void updateVAF2_VBI2(Map map) throws Exception {
		hosSendAdviceDao.updateVAF2_VBI2(map);
	}

	private void insertVAI2_VAJ2_VAJT(Map map) throws Exception {
		hosSendAdviceDao.insertVAI2_VAJ2_VAJT(map);
	}

	private List getTempVBD(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getVBD2(map);
		for (Map<String, Object> mm : ls) {
			long newID = getNewId.nextId();
			mm.put("vbd01", newID);
		}
		return ls;

	}

	private List getTmpVAV2(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getTmpVAV2(map);
		for (Map<String, Object> mm : ls) {
			long newID = getNewId.nextId();
			mm.put("vav01", newID);
		}
		return ls;
	}

	private List getTmpVAT(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getTmpVAT(map);
		for (Map<String, Object> mm : ls) {
			long newID = getNewId.nextId();
			mm.put("vat01", newID);
		}
		return ls;
	}

	private List tmpVAI(Map map) throws Exception {
		return hosSendAdviceDao.tmpVAI(map);
	}

	private List tmpVAFzt(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getTmpVAFzt(map);
		for (Map<String, Object> mm : ls) {
			long newID = getNewId.nextId();
			mm.put("vbi01", newID);
			mm.put("vbi03", newID);
			newID = getNewId.nextId();
			mm.put("vbg01", newID);
		}
		return ls;

	}

	private List tempVAJ(Map map) throws Exception {
		return hosSendAdviceDao.tempVAJ(map);

	}

	private List getVAJ2(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getVAJ2(map);
		for (Map<String, Object> mm : ls) {
			long newID = getNewId.nextId();
			mm.put("vaj01", newID);
			mm.put("bag46", 0);
		}
		return ls;
	}

	private List getVBI2(Map map) throws Exception {
		List<Map<String, Object>> ls = hosSendAdviceDao.getVAI2(map);
		for (Map<String, Object> mm : ls) {
			long newID = getNewId.nextId();
			mm.put("vai01", newID);
			mm.put("vai04", "LZ" + newID);
			mm.put("vai22", 0);
			mm.put("vai23", 0);
		}
		return ls;
	}
	/*
	 public ModelVo otherAdviceList(Map map,int currPage,int pageSize)throws Exception{
		 ModelVo mo = new ModelVo();
			Page pg = new Page();
			try {
				String lxml = map.get("lxml").toString();
				String lBCKxml = map.get("lBCKxml").toString();
				JSONObject jsonVAF2 = JSONObject.fromObject(lxml);
				JSONObject jsonBCK1 = JSONObject.fromObject(lBCKxml);
//				List<Map<String, Object>> VAE1 = JsonUtil.parserToList(jsonVAF2.getJSONArray("VAE1"));
				List<Map<String, Object>> BDA1 = JsonUtil.parserToList(jsonVAF2.getJSONArray("BDA1"));
				List<Map<String, Object>> BCK1 = JsonUtil.parserToList(jsonBCK1.getJSONArray("BCK1"));
				if ( BDA1.isEmpty() || BCK1.isEmpty()) {
					mo.setResult(StaticKeys.OH_FALSE);
					mo.setResultMsg("提交的数据不能为空");
					return mo;
				}
				List<Map<String, Object>> BDAO = hosSendAdviceDao.getBDAOther2(map);
//				map.put("VAE1", VAE1);
				map.put("BDA1", BDA1);
				map.put("BCK1", BCK1);
                map.put("BDAO", BDAO);
				String lEDate = DateUtils.addInteger(map.get("lEndDate").toString(), Calendar.DATE, 1);
				map.put("lEDate", lEDate);
				map.put("lVAF38", map.get("lEndDate").toString());

				pg = hosSendAdviceDao.otherAdviceList(map, currPage, pageSize);

			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(pg.getList());
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(pg.getList());
			return mo;
		 
		 
	 }
	 */
	 public ModelVo nurseTempAdviceSendJudge(Map map)throws Exception{
		 ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		 return mo;
	 }

	private List<Map<String,Object>> getTmpVAFzt(JSONObject job,Map<String,Object> map)throws Exception{
		List<Map<String,Object>> list = JsonUtil.parserToList(job.getJSONArray("VAF1"));
		List<Map<String,Object>> rlist = null;
		if(list!=null&&list.size()>0) {
			map.put("vafList", list);
			rlist = hosSendAdviceDao.queryForList("HosSendAdvice.bills_print_001",map);
			if(rlist!=null&&rlist.size()>0){
				Iterator<Map<String,Object>> it = rlist.iterator();
				while(it.hasNext()){
					Map<String,Object> itmap = it.next();
						String bda01 = MapUtils.getString(itmap,"bda01");
						if(!("L".equals(bda01)||"E".equals(bda01)||"S".equals(bda01)||"Z".equals(bda01))){
							it.remove();
						}
					}
				}
		}
		return rlist;
	}
	//全部发送检测
	private List<Map<String,Object>> getTmpVAFztall(Map<String,Object> map)throws Exception{
		List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("vaf");
		List<Map<String,Object>> rlist = null;
		if(list!=null&&list.size()>0) {
			map.put("vafList", list);
			rlist = hosSendAdviceDao.queryForList("HosSendAdvice.bills_print_001",map);
			if(rlist!=null&&rlist.size()>0){
				Iterator<Map<String,Object>> it = rlist.iterator();
				while(it.hasNext()){
					Map<String,Object> itmap = it.next();
						String bda01 = MapUtils.getString(itmap,"bda01");
						if(!("L".equals(bda01)||"E".equals(bda01)||"S".equals(bda01)||"Z".equals(bda01))){
							it.remove();
						}
					}
				}
		}
		return rlist;
	}
	private String existsVaf(List<Map<String,Object>> vafs,String bda01){
		String result = "";
		for(int i=0;i<vafs.size();i++){
			Map<String,Object> objMap = vafs.get(i);
			String t_bda01 = MapUtils.getString(objMap,"bda01");
			String vaf01 = MapUtils.getString(objMap,"vaf01");
			if(bda01.equals(t_bda01)){
				if(i<vafs.size()-1) {
					result += vaf01 + ",";
				}else{
					result += vaf01;
				}
			}
		}
		return result;
	}

	private List<Map<String,Object>> getTmpBEL(Map<String,Object> map)throws Exception{
		String lBDA01 = MapUtils.getString(map,"lBDA01");
		List<Map<String,Object>> tmpBEL = null;
		if("L".equals(lBDA01)){
			if((List)(map.get("vafList"))!=null&&((List) map.get("vafList")).size()>0)
				tmpBEL = hosSendAdviceDao.queryForList("HosSendAdvice.bills_print_003",map);
		}else if("E".equals(lBDA01)){
			if((List)(map.get("vafList"))!=null&&((List) map.get("vafList")).size()>0)
				tmpBEL = hosSendAdviceDao.queryForList("HosSendAdvice.bills_print_004",map);
		}else if("S".equals(lBDA01)||"Z".equals(lBDA01)){
			if((List)(map.get("vafList"))!=null&&((List) map.get("vafList")).size()>0)
				tmpBEL = hosSendAdviceDao.queryForList("HosSendAdvice.bills_print_005",map);
		}
		return tmpBEL;
	}

	private List<Map<String,Object>> getKbmvaf(Map<String,Object> map)throws Exception{
		List<Map<String,Object>> rlist = hosSendAdviceDao.queryForList("HosSendAdvice.bills_print_002",map);
		Iterator<Map<String,Object>> it = rlist.iterator();
		List<Map<String,Object>> tmpVAFid = (List<Map<String,Object>>)MapUtils.getObject(map,"vafList");
		Integer vae44 = MapUtils.getInteger(map,"VAE44",4);
		Iterator<Map<String,Object>> it2 = tmpVAFid.iterator();
		while(it.hasNext()){
			Map<String,Object> objMap = it.next();
			Long vaf51 = MapUtils.getLong(objMap,"vaf51");
			Long vaf01 = MapUtils.getLong(objMap,"vaf01");
			Long vaf01a = MapUtils.getLong(objMap,"vaf01a");
			Integer apart = MapUtils.getInteger(objMap,"apart",0);

			if(vaf51!=null&&vaf51.intValue()>0){
				it.remove();
				while(it2.hasNext()){
					Map<String,Object> oo = it2.next();
					Long vafid = MapUtils.getLong(oo,"vaf01");
					if(vafid!=null&&(vafid.longValue()==vaf01.longValue()||vafid.longValue()==vaf01a.longValue())){
						it2.remove();
					}
				}
			}
			if(vae44.intValue()==5&&apart.intValue()==2){
				map.put("aPart",2);
			}
		}
		return rlist;
	}
	private String getLstr(int xhLen){
		String result = "";
		for(int k = 0;k<xhLen;k++)
		{
			result += "0";
		}
		return result;
	}
	private int getCbw03(Map map)throws Exception{
		int cbw03 = 0;
		Integer incCount = MapUtils.getInteger(map,"aMax",1);

		List temp1List = hosSendAdviceDao.queryForList("HosSendAdvice.bills_print_006", map);
		List temp2List = hosSendAdviceDao.queryForList("HosSendAdvice.bills_print_007", map);
		if(temp1List!=null&&temp1List.size()>0){
			map.put("incCount",incCount);
			cbw03 = ((Integer)hosSendAdviceDao.getSqlSession().selectOne("HosSendAdvice.bills_print_009",map)).intValue();
		}else{
			if(temp2List!=null&&temp2List.size()>0){
				cbw03 = ((Integer)hosSendAdviceDao.getSqlSession().selectOne("HosSendAdvice.bills_print_008",map)).intValue();
				incCount = cbw03+incCount;
			}
			map.put("incCount",incCount);
			map.put("cbw01",UUID.randomUUID().toString().toUpperCase());
			hosSendAdviceDao.insert("HosSendAdvice.bills_print_010",map);
			cbw03 = incCount;
		}
		return cbw03;
	}

	/**返回条形码序号 Core_NewId_CBW1
    	条形码生成规则:
      1-6位: YYMMDD
      7-8位: 系统代码  门诊病人以1开头，住院病人以2开头，体检病人以4开头  9：其他
      9-14位: 序列号
      系统代码：99:其他(非我们的his 、体检系统病人) 11:门诊收费 21：住院收费、12：门诊药房 22:住院药房
        、13:门诊医师站 23:住院医师、护士站、14:检验门诊病人，24：检验住院病人，44：检验体检病人
        45:体检系统条码 16:ris门诊病人 26：ris住院病人 46:ris体检病人 17:门诊输液  80：质控条码
        27:护士站瓶签条码
    */
	private Map<String,Object> coreNewIdCBW1(Map<String,Object> map)throws Exception{
		String gpara79 = hosParamsDao.getSysValue(9999,9999,79);
		String gpara89 = hosParamsDao.getSysValue(9999,9999,89);
		String apara1 = MapUtils.getString(map,"apara1","");
		Integer aMax = MapUtils.getInteger(map,"aMax",0);
		Map<String,Object> rMap = new HashMap<String,Object>();
		String vPrefix = "";
		gpara89 = (gpara89==null?"":gpara89);
		gpara79 =  (gpara79==null?"0":gpara79);
		Integer gpara79_num = Integer.parseInt(gpara79);
		if(gpara79_num==0){
			gpara79_num = 14;
		}
		String cBW02 = new SimpleDateFormat("yyMMdd").format(new Date());
		vPrefix = cBW02+apara1;
		if(gpara89.length()==2){
			vPrefix = gpara89+vPrefix;
		}
		int xhLen = 6;
		if(gpara79_num==12){
			xhLen = 4;
		}
		if(gpara79_num==13){
			xhLen = 5;
		}
		if("27".equals(apara1)){
			xhLen = 6;
		}
		int lid = getCbw03(map);
		lid = lid-aMax;
		rMap.put("vPrefix",vPrefix);
		rMap.put("xhLen",xhLen);
		rMap.put("lid",lid);

		return rMap;
	}
	//Nurse_Lis_Bills_Print 打印检验申请单(住院护士)
	public ModelVo nurseLisBillsPrint(Map<String,Object> map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		Integer lACF01 = MapUtils.getInteger(map,"lACF01",2);
		String lBDA01 = MapUtils.getString(map,"lBDA01","");
		String lVAFid = MapUtils.getString(map,"lVAFid","");
		String Ssvaf = lVAFid.substring(lVAFid.length()-1,lVAFid.length());
		if(Ssvaf.equals(","))
		{
			lVAFid=lVAFid.substring(0,lVAFid.length()-1);
			map.put("lVAFid",lVAFid);
		}
		int aPart = 1;
		map.put("aPart",aPart);
		List<Map<String,Object>> tmpVAFid = new ArrayList<Map<String,Object>>();
		if(lACF01.intValue()==2){
			String[] arr = lVAFid.split(",");
			for(String vid:arr){
				Map<String,Object> oo = new HashMap<String,Object>();
				oo.put("vaf01",Long.parseLong(vid));
				tmpVAFid.add(oo);
			}
			map.put("vafList",tmpVAFid);
			List<Map<String,Object>> kbmvaf = getKbmvaf(map);
			map.put("kbmvaf",kbmvaf);
			aPart = MapUtils.getInteger(map,"aPart",1);
			List<Map<String,Object>> tempBEL = getTmpBEL(map);
			if(tempBEL!=null&&tempBEL.size()>0){
				 String apara1 = "23";
				 map.put("apara1",apara1);
				 int aMax = tempBEL.size();
				 map.put("aMax",aMax);
				 Map<String,Object> rMap = coreNewIdCBW1(map);
				 Integer xhLen = MapUtils.getInteger(rMap,"xhLen",0);
				 Integer lid = MapUtils.getInteger(rMap,"lid",0);
				 String vPrefix = MapUtils.getString(rMap,"vPrefix","");
				 String lStr = getLstr(xhLen);


				 Long lVAF06 = 0l;
				 String lBEL01 = "";
				 Long lBCK01 = 0l;
				 Integer lVAF35 = 0;
				 String lVAF14 = "";
				 String BBC01 = "";
				 Long VAF01 = 0l;
				 Long lBCE01A = 0l;
				 Long lvap01 = 0l;
				 for(int i=0;i<tempBEL.size();i++){
					Map<String,Object> bel = tempBEL.get(i);
					bel.put("vbi21",lid+(i+1));

					lVAF06 = MapUtils.getLong(bel,"vaf06",0l);
					lBEL01 = MapUtils.getString(bel,"bel01","");
					lBCK01 = MapUtils.getLong(bel,"bck01b",0l);
					lVAF35 = MapUtils.getInteger(bel,"vaf35",0);
					lVAF14 = MapUtils.getString(bel,"vaf14","");
					String lidi = MapUtils.getString(bel,"vbi21","");
					BBC01 = MapUtils.getString(bel,"bbx13","");
					VAF01 = MapUtils.getLong(bel,"sVAF01",0l);
					lBCE01A = MapUtils.getLong(bel,"bce01a",0l);
					lvap01 = MapUtils.getLong(bel,"vaf07",0l);

					map.put("lVAF06",lVAF06);
					map.put("lBEL01",lBEL01);
					map.put("lBCK01",lBCK01);
					map.put("lVAF35",lVAF35);
					map.put("lVAF14",lVAF14);
					map.put("BBC01",BBC01);
					map.put("VAF01",VAF01);
					map.put("lBCE01A",lBCE01A);
					map.put("lvap01",lvap01);
					
					String lVBI21 =vPrefix + StringUtils.substring(lStr,1,lStr.length()-(lidi+"").length())+lidi;
					String lBCE03A = (String)hosSendAdviceDao.queryToObject("HosSendAdvice.bills_print_011", map);	//获取开嘱医生姓名
					map.put("lVBI21",lVBI21);
					map.put("lBCE03A",lBCE03A);
					map.put("lDateG",new Date());
					map.put("lVBI29",0);
					if("L".equals(lBDA01)){
						String lVBQ12 = (String)hosSendAdviceDao.queryToObject("HosSendAdvice.bills_print_012", map);	//获取医嘱开始时间
						map.put("lVBQ12",lVBQ12);
						map.put("lVBQ01",getNewId.nextId());
						hosSendAdviceDao.insert("HosSendAdvice.bills_print_013", map);	//插入诊疗申请单VBQ2
						hosSendAdviceDao.update("HosSendAdvice.bills_print_014", map);	//更新医嘱单诊疗申请单ID
						hosSendAdviceDao.update("HosSendAdvice.bills_print_015", map);	//更新医嘱单抄送记录VBI2
						if(aPart==2){
							hosSendAdviceDao.update("HosSendAdvice.bills_print_016", map);	//更新离线医嘱记录VAF2_2
							hosSendAdviceDao.update("HosSendAdvice.bills_print_017", map);	//更新离线医嘱单抄送记录VBI2_2
						}
					}else if("E".equals(lBDA01)){
						String lVBQ12 = (String)hosSendAdviceDao.queryToObject("HosSendAdvice.bills_print_018", map);	//获取医嘱开始时间
						map.put("lVBQ12",lVBQ12);
						map.put("lVBQ01",getNewId.nextId());
						hosSendAdviceDao.insert("HosSendAdvice.bills_print_013", map);	//插入诊疗申请单VBQ2
						hosSendAdviceDao.update("HosSendAdvice.bills_print_019", map);	//更新医嘱单诊疗申请单ID
						hosSendAdviceDao.update("HosSendAdvice.bills_print_020", map);	//更新医嘱单抄送记录VBI2
						if(aPart==2){
							hosSendAdviceDao.update("HosSendAdvice.bills_print_021", map);	//更新离线医嘱记录VAF2_2
							hosSendAdviceDao.update("HosSendAdvice.bills_print_022", map);	//更新离线医嘱单抄送记录VBI2_2
						}
					}else if("S".equals(lBDA01)||"Z".equals(lBDA01)){
						String lVBQ12 = (String)hosSendAdviceDao.queryToObject("HosSendAdvice.bills_print_023", map);	//获取医嘱开始时间
						map.put("lVBQ12",lVBQ12);
						map.put("lVBQ01",getNewId.nextId());
						hosSendAdviceDao.insert("HosSendAdvice.bills_print_013", map);	//插入诊疗申请单VBQ2
						hosSendAdviceDao.update("HosSendAdvice.bills_print_024", map);	//更新医嘱单诊疗申请单ID
						hosSendAdviceDao.update("HosSendAdvice.bills_print_025", map);	//更新医嘱单抄送记录VBI2
						if(aPart==2){
							hosSendAdviceDao.update("HosSendAdvice.bills_print_026", map);	//更新离线医嘱记录VAF2_2
							hosSendAdviceDao.update("HosSendAdvice.bills_print_027", map);	//更新离线医嘱单抄送记录VBI2_2
						}
					}

				}	
			 }
				

		}
		return mo;
	}

	//Nurse_Lis_Bills_Print_Auto 护士站发送医嘱后自动生成医技申请单条码
	public ModelVo nurseLisBillsPrintAuto(Map map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		Long lBCE01 = MapUtils.getLong(map,"lBCE01"); //员工ID
		Long lBCE02 = MapUtils.getLong(map,"lBCE02"); //当前操作员编号
		String lBCE03 = MapUtils.getString(map,"lBCE03");  //当前操作员姓名
		String lxml = MapUtils.getString(map,"lXml"); //获取医嘱相关json信息字符串
		Integer lACF01 = MapUtils.getInteger(map,"lACF01");
		if(StringUtils.isBlank(lxml)){
			return new ModelVo(StaticKeys.OH_SUCCESS, "自动生成条码信息为空！");
		}
		JSONObject job = JSONObject.fromObject(lxml);
		if(job==null||job.isEmpty()){
			return new ModelVo(StaticKeys.OH_SUCCESS, "提交信息错误，自动生成条码相关信息格式不正确，请检查！");
		}
		List<Map<String,Object>> tmpVAFzt = getTmpVAFzt(job,map);
		if(tmpVAFzt==null||tmpVAFzt.size()==0){
			return new ModelVo(StaticKeys.OH_SUCCESS, "自动生成条码医嘱信息只能为检验单/检查单/手术通知单/会诊单，请检查！");
		}
		int num = 1;
		String BDA01 = "";
		String sVafid = "";
		String VBI36 = "";
		Date adt = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String s = df.format(adt);
		String lDateB = s+".000";
		String lDateE = s+".999";

		while(num<=4){
			switch(num){
				case 1: BDA01 = "L";VBI36 = "检验单";break;
				case 2: BDA01 = "E";VBI36 = "检查单";break;
				case 3: BDA01 = "S";VBI36 = "手术通知单";break;
				case 4: BDA01 = "Z";VBI36 = "会诊单";break;
				default:break;
			}
			sVafid= existsVaf(tmpVAFzt,BDA01);
			if(org.apache.commons.lang.StringUtils.isNotBlank(sVafid)){
				map.put("lVAFid",sVafid);
				map.put("lDateB",lDateB);
				map.put("lDateE",lDateE);
				map.put("lBCE03",lBCE03);
				map.put("lBDA01",BDA01);
				map.put("lBAU01","31");
				map.put("lACF01",2);
				map.put("lCBM01",0);
				map.put("VBI36",VBI36);
				map.put("VAE44",4);
				mo = nurseLisBillsPrint(map);
			}
			num++;
		}
		return mo;
	}

//Nurse_Lis_Bills_Print_Auto 全部发送时判断是否生成医技条码
	public ModelVo nurseLisBillsPrintAutoall(Map map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		Long lBCE01 = MapUtils.getLong(map,"lBCE01"); //员工ID
		Long lBCE02 = MapUtils.getLong(map,"lBCE02"); //当前操作员编号
		String lBCE03 = MapUtils.getString(map,"lBCE03");  //当前操作员姓名
		Integer lACF01 = MapUtils.getInteger(map,"lACF01");

		List<Map<String,Object>> tmpVAFzt = getTmpVAFztall(map);
		if(tmpVAFzt==null||tmpVAFzt.size()==0){
			return new ModelVo(StaticKeys.OH_SUCCESS, "自动生成条码医嘱信息只能为检验单/检查单/手术通知单/会诊单，请检查！");
		}
		int num = 1;
		String BDA01 = "";
		String sVafid = "";
		String VBI36 = "";
		Date adt = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String s = df.format(adt);
		String lDateB = s+".000";
		String lDateE = s+".999";

		while(num<=4){
			switch(num){
				case 1: BDA01 = "L";VBI36 = "检验单";break;
				case 2: BDA01 = "E";VBI36 = "检查单";break;
				case 3: BDA01 = "S";VBI36 = "手术通知单";break;
				case 4: BDA01 = "Z";VBI36 = "会诊单";break;
				default:break;
			}
			sVafid= existsVaf(tmpVAFzt,BDA01);
			if(org.apache.commons.lang.StringUtils.isNotBlank(sVafid)){
				map.put("lVAFid",sVafid);
				map.put("lDateB",lDateB);
				map.put("lDateE",lDateE);
				map.put("lBCE03",lBCE03);
				map.put("lBDA01",BDA01);
				map.put("lBAU01","31");
				map.put("lACF01",2);
				map.put("lCBM01",0);
				map.put("VBI36",VBI36);
				map.put("VAE44",4);
				mo = nurseLisBillsPrint(map);
			}
			num++;
		}
		return mo;
	}
	@SuppressWarnings("unchecked")
	public String getParameter(Integer productId, Integer programId, Integer paramNo)
			throws Exception
	{
		HashMap map = new HashMap();
		map.put("productId", productId);
		map.put("programId", programId);
		map.put("paramNo", paramNo);
		return cliSendAdviceDao.getParameter(map);
	}
}